2012-03-30 46 views
0


大家好!
这个问题一直在窃听几天,我不能想出一个解决方案。
我知道如何使用Titanium在Android上执行单个预定通知。
但是当我需要多个预定的通知时,问题就会出现。
当我试图发出第二个通知时,第一个仍在计划中的第一个通知立即弹出。
而第二个通知从未解雇。
我真的很想知道是否有一种方法可以在Android上使用Titanium创建多个预定通知,或者实现预定通知的更好方法,而不是现在使用的方法。安卓android应用程序多预定android通知

谢谢您的阅读!

我使用这里发现的代码来形成我的实现。
https://github.com/appcelerator-developer-relations/Forging-Titanium/tree/master/ep-013/notify

这是我用来为通知创建服务的函数。

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); 

} 

这是我的NotificationService.js文件中的代码。

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); 
} 

回答

1

我有它什么都不做的Ti.Android.stopService(intent);Ti.Android.startService(intent);

第一次启动。在第二秒钟,它阻止启动第一个。

我不认为这是一个很好的解决方案,但也许作品