【问题标题】:Local notification in android from database来自数据库的android中的本地通知
【发布时间】:2016-06-15 09:10:47
【问题描述】:

我的要求是在当前时间与数据库中的值匹配时生成本地通知。但现在的问题是,当我单击通知时,它应该使用产品 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);
            }

【问题讨论】:

    标签: android notifications click local


    【解决方案1】:

    当您生成通知时,intent 中的产品标题/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);
    

    【讨论】:

    • 我刚刚发布了我的代码..如何在点击通知时获取 id
    • 将 id 添加到 Intent 中,如下所示:notificationIntent.putExtra("id", YOUR_ID);
    • 是的,我已经这样做了,但它返回的 id 与最后一个相同。因为它在 for 循环中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2022-12-07
    • 2011-07-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多