2014-11-08 34 views
1

我想在android中爲我的通知添加一個按鈕。 我使用addAction方法來添加一個意圖,該意圖假設打開主要活動(與單擊整個通知相同),但是會附加一個包含數據的包。 這是我迄今所做的:通知addAction創建沒有行爲的按鈕

notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); 

//regular intent to view main activity 
PendingIntent contentIntent = PendingIntent.getActivity(this,Constants.MAIN_ACTIVITY, 
           new Intent(this, MainActivity.class), 0); 

//intent for viewing transaction dialog, within main activity using PURCHASE_DIALOG request code 
Bundle bundle = new Bundle(); 
bundle.putParcelableArrayList(Constants.LIST, (java.util.ArrayList<? extends android.os.Parcelable>) list); 
PendingIntent purchaseIntent = PendingIntent.getActivity(
           this, Constants.PURCHASE_DIALOG, 
           new Intent(this, MainActivity.class), 0, bundle); 

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setContentTitle(Constants.NOTIFICATION_TOPIC) 
         .setStyle(new NotificationCompat.BigTextStyle() 
         .bigText(watchResponse.toString())) 
         .setAutoCancel(true) 
         .addAction(R.drawable.ic_launcher, "Buy", purchaseIntent) 
         .setContentText(message); 

mBuilder.setContentIntent(contentIntent); 
notificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 

我只是希望我的onResumeMainActivity類將被調用一次點擊多餘的動作,並有我就可以拿到束槽getIntent(),但當我點擊它時沒有任何反應。該按鈕被點擊,但活動仍然打開,我的應用程序活動不會啓動。

回答

0

我有一個非常相似的問題,但一個非常不同的解決方案。如果您在manifest.xml文件中聲明瞭<service android:enabled="false"></service>,那麼等待意圖也不會被解僱。

替換從android:enabled="false"android:enabled="true"

這可能不是問題的直接問題。但是,如果您使用默認模板在android studio中創建服務,它會自動將這些屬性添加到服務中。

如果這不起作用,您可以在這裏找到類似的問題: Android Notification Action is not fired (PendingIntent)