android中的数据库本地通知

时间:2016-06-15 09:10:48

标签: android notifications click local

我的要求是在当前时间与数据库中的值匹配时生成本地通知。但现在的问题是,当我点击通知时,它应该使用产品ID来获取特定的产品详细信息。

我已生成通知,但如何将产品标题/ ID提供给产品详细信息页面。

代码----------------

for (int i=0;i < alertList.size();i++) {
                Random random = new Random();
                int m = random.nextInt(9999 - 1000) + 1000;
                msg=alertList.get(i).getProf_name()+" have to take "+alertList.get(i).getMed_name()+"                                                                                          ;"+alertList.get(i).getMed_id();
                Log.d("MediMSg",""+msg);
                Log.d("MediMSg",""+msg);
                Intent notificationIntent = new Intent(context, MedicineNotificationActivity.class);
                notificationIntent.putExtra("pushmsg",msg);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MedicineNotificationActivity.class);
                stackBuilder.addNextIntent(notificationIntent);
                PendingIntent pendingIntent = stackBuilder.getPendingIntent(100, PendingIntent.FLAG_UPDATE_CURRENT);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                Notification notification = builder.setContentTitle("Test")
                        .setContentText(msg)
                        .setTicker("Notification from Test")
                        .setSmallIcon(getNotificationIcon())
                        .setPriority(Notification.PRIORITY_MAX)
                        .setAutoCancel(true)
                        .setColor(0x0091ea)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setContentIntent(pendingIntent).build();

                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.notify(m,notification);
                Log.d("Random",""+m);
            }

1 个答案:

答案 0 :(得分:0)

当您生成通知时,意图中的产品标题/ ID作为附加内容。然后在您通过单击通知启动的活动中。你可以访问这些额外内容。

如果你有多个ID,你可以在循环外创建一个数组,如下所示:

int[] ids = new int[alertList.size()];

然后在循环中,迭代并为每个索引设置每个索引的id。

for (int i=0;i < alertList.size();i++) {
     ids[i] = YOUR_ID;
     //OTHER Code
}

最后,加上这样的额外内容:

notificationIntent.putExtra("ids", ids);