我们短信通知时如何捕获通知ID

时间:2016-07-01 12:10:01

标签: android notifications

以下是我的应用程序中通知的代码段。这个结果是每条消息的新通知。但我需要一条通知来处理多条消息。

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);

 nBuilder.setSmallIcon(R.drawable.app_icon_indietext_new)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon_indietext_new))
                        .setContentTitle(senderNum)
                        .setContentText(message)
                        .setAutoCancel(true);

NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            //String Number = nBuilder.toString();

            //create an intent to open  a required activity
           Intent nIntent = new Intent(context, ConvActivity.class);
           nIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            //nIntent.putExtra(_id, threadId);
           nIntent.putExtra(ADDRESS, senderNum);

            //click action  will  be done by  pendingIntent.send intent to the pendingIntent
            PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            nBuilder.addAction(R.drawable.replyyyy, "Reply", nPendingInten);
            String ThreadID = (String) nBuilder.getExtras().get(_id);

            Random random = new Random();
            int m = random.nextInt(9999 - 1000) + 1000;

            NotificationManager nNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            TaskStackBuilder nStackBuilder = TaskStackBuilder.create(context);
            nStackBuilder.addParentStack(ConvActivity.class);

            nStackBuilder.addNextIntent(nIntent);
            nBuilder.setContentIntent(nPendingInten);

            Notification n = nBuilder.build();
            n.defaults |= Notification.DEFAULT_ALL;
            nNotificationManager.notify(m,n);

并且,当我打开我的应用程序时,首先是消息列表。当我点击项目时它会打开对话。这里很好。如果发件人已经在list.how中,我想打开同一个对话这是可能的。

1 个答案:

答案 0 :(得分:1)

我建议您为每个uniq用户使用uniq标识符,并根据该标识符进行通知,如果仅在更新时存在,则创建新的标识符。也许你可以使用下面的代码将发件人名称转换为数字序列,并将其用作id:

String str = "abc";  

StringBuilder sb = new StringBuilder(); for (char c : str.toCharArray()) sb.append((int)c);

你的m值:

 BigInteger m = new BigInteger(sb.toString()); 

希望我已正确理解你的答案,并帮助你:)