FishTruck
FishTruck

Reputation: 18

multiple scheduled android notification on android with appcelerator titanium


Hello everyone!
This problem has been bugging for days and I can't come up with a solution.
I know how to do a single scheduled notification on android with Titanium.
But when I need more than one scheduled notification the problem appears.
When I tried to fire second notification the first one that was still in schedule pops out immediately.
And the second notification never fired.
I would really like to know if there is a way to create more than one scheduled notification with Titanium on Android, or a better method to implement scheduled notification than I am using now.

Thank you for reading!

I use the code found here to form my implementaion.
https://github.com/appcelerator-developer-relations/Forging-Titanium/tree/master/ep-013/notify

This is the function I use to create a service for a notification.

var notifyId=0;
var notify=function(message,interval){
var intent = Ti.Android.createServiceIntent({
    url : 'NotificationService.js'
});

intent.putExtra('message', message || 'You have a notification!');
intent.putExtra('notifyId',notifyId+'');
notifyId++;
if (interval) {
    intent.putExtra('interval', interval);
}

Ti.Android.startService(intent);

}

This is the code in my NotificationService.js file.

var NOTIFICATION_PROPERTY = 'notificationCount';

var service = Ti.Android.currentService;//maybe problem is here
var serviceIntent = service.getIntent();
var serviceMessage = serviceIntent.hasExtra('message') ? serviceIntent.getStringExtra('message') : 'you have a notification!';
var notifyId=serviceIntent.hasExtra('notifyId')?serviceIntent.getStringExtra('notifyId'):'1';
if (serviceIntent.hasExtra('interval') && !Ti.App.Properties.hasProperty('notificationCount')) {
    Ti.App.Properties.setInt(NOTIFICATION_PROPERTY,0);
} else{
if (Ti.App.Properties.hasProperty(NOTIFICATION_PROPERTY)) {
Ti.App.Properties.removeProperty(NOTIFICATION_PROPERTY);
}

var activity = Ti.Android.currentActivity;
var intent = Ti.Android.createIntent({
    action : Ti.Android.ACTION_MAIN,

    className:'com.fishtruck.Activity',
    flags : Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
});
intent.addCategory(Titanium.Android.CATEGORY_LAUNCHER);

var pending = Ti.Android.createPendingIntent({
    activity : activity,
    intent : intent,
    type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
    flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
});

var notification = Ti.Android.createNotification({
    contentIntent : pending,
    contentTitle : 'test',
    contentText : serviceMessage,
    tickerText : serviceMessage,
    when : new Date().getTime(),
    icon : Ti.App.Android.R.drawable.appicon,
    flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
});

Ti.Android.NotificationManager.notify(parseInt(notifyId), notification);

Ti.Android.stopService(serviceIntent);
}

Upvotes: 0

Views: 1273

Answers (1)

GTN
GTN

Reputation: 21

I have a Ti.Android.stopService(intent); before Ti.Android.startService(intent);

On first launch it does nothing. On sencond, it prevents to launch the first one.

I don't think that it is a good solution, but maybe works

Upvotes: 2

Related Questions