2012-03-30 45 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);

第一次啓動。在第二秒鐘,它阻止啓動第一個。

我不認爲這是一個很好的解決方案,但也許作品