2013-08-07 237 views
1

我目前正在为我的应用做一些通知。现在我已经成功创建了一个通知,但问题在于,我的每个通知都不会在点击时传递不同的附加内容。Android通知PendingIntent每通知点击打开活动

那么会发生什么,我只有一个循环,每循环调用createNotification方法,并且我还为这些通知分配了不同的ID以显示在不同的堆栈中。

这里是我的代码:

private void createNotification(String record_name, String file_path, String status, int id) { 
     /*must pass: 
     * record_name, (get the record name in DB using the id create a new query method) 
     * record_status, (simple part just match if equivalent to ready to download for the JSON data then show notif if true) 
     * record_path *full path* (this one can be get using the record name itself add the path and extension name)*/ 

     /*this will jump from the current activity into the target activity class just do some workarounds here*/ 
     Intent intent = new Intent(this, RecordEditActivity.class); 
     intent.putExtra("record_name", record_name); 
     intent.putExtra("record_path", file_path); 
     intent.putExtra("record_status", status); 
     PendingIntent pIntent = PendingIntent.getActivity(this,0, intent, 0); 


     /*build the notification here this is only supported for API 11. Since we've targeted API 11 there will be no problem on this*/ 
     NotificationCompat.Builder notify = new NotificationCompat.Builder(this) 
       .setContentTitle(record_name + " is ready for download") 
       .setContentText("Click to view or download recording") 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.notif_icon) 
       .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher)) 
       .setTicker("Echo: your file is ready for download") 
       .setContentIntent(pIntent); 


     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     if(Build.VERSION.SDK_INT<16){ 
      /*build notification for HoneyComb to ICS*/ 
      notificationManager.notify(id, notify.getNotification()); 
     }if(Build.VERSION.SDK_INT>15){ 
      /*Notification for Jellybean and above*/ 
      notificationManager.notify(id, notify.build()); 
     } 

    } 

我认为这个问题是在PendingIntent的部分,因为我不知道我应该使用哪一个标志为这一点,但我没有那么肯定了。

+0

你试过我的回答吗?当然这会帮助你.. –

+0

在你的回答中添加了评论。 – KaHeL

回答

4

明白了!我需要为我的pendingIntent requestCode分配一个ID,我所做的是分配与我的notificationManager相同的ID。

更多信息here

0

只需使用的代码 -

PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

这一行而对于更详细看到这样的回答: GCM android Push Notification shows old message always. Intent received incorrect

+0

试过这一个已经发生了什么是我通过我的通知2数据。在我的第一个代码中,标志是0,被调用的意图只是在循环中传递的第一个数据。可以说我有record2和记录3.显示的内容是2通知,但是这两个通知都会将我重定向到同一个活动intent(记录2)。应用FLAG_UPDATE_CURRENT后,两个通知都会将我重定向到record3,而不是我想要的方式。 – KaHeL

+0

好吧,我得到了..你想要两个通知吗? 1,2,3等等吧? –

+0

那么为了简化事情,我需要的是我的每个通知都有一个正确的未决意图。每个应该是唯一的。无论如何,已经得到了解决方案。谢谢! :) – KaHeL