每次通知点击Android通知PendingIntent打开活动

时间:2013-08-07 04:39:37

标签: android

我正在为我的应用做一些通知。现在我已经成功创建了一个通知,但问题是我的每个通知都没有在点击时传递不同的额外内容。

发生的事情是,我只有一个循环,每个循环调用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部分,因为我不知道我应该使用哪些标志,但我不确定。

2 个答案:

答案 0 :(得分:4)

知道了!我需要为我的pendingIntent requestCode分配一个ID,我所做的是分配我用于我的notificationManager的相同ID。

更多信息here

答案 1 :(得分:0)

只需使用这一行代码 -

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

有关详细信息,请参阅此答案: GCM android Push Notification shows old message always. Intent received incorrect