1

我目前正在为我的应用程序做一些通知。现在我已经成功创建了一个通知,但问题是我的每个通知在点击时都没有传递不同的附加信息。

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

4

2 回答 2

4

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

更多信息在这里

于 2013-08-07T06:08:12.030 回答
0

只需使用这一行代码-

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

有关更多详细信息,请参阅此答案: GCM android Push Notification shows old message always。收到的意图不正确

于 2013-08-07T05:03:15.283 回答