更新通知文本

Update notifcation text

我目前已经设置了一个小应用程序,可以在设备上设置警报并将相关信息存储在 SQL 数据库中,例如闹钟名称和时间。当警报被激活时,我的应用程序会向用户发送通知。目前每个通知都是静态的,这意味着每条消息都是相同的。我现在想允许我的应用程序获取已激活的警报的名称并将其显示在通知中。现在我知道,在设置多个警报时,您需要多个 ID,我已将其存储在 SQL 的一侧,我猜在发送通知时也是如此。有没有办法将我的警报 ID 与通知上的警报 ID 相匹配,以便它知道要发送什么消息,例如闹钟名称?

设置闹钟的当前代码

pendingIntent = PendingIntent.getBroadcast(view.getContext(), Integer.valueOf(alarm_request_code), receiver, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.set(AlarmManager.RTC_WAKEUP, myCalendar.getTimeInMillis(), pendingIntent);

intentArrayList.add(pendingIntent);

我的广播接收器...

NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);


    Intent moveActivity = new Intent(context, AlarmActivity.class);


    moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle("Standard text")
            .setContentText("Standard text")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true);


    notificationManager.notify(0, builder.build());



}

//////////////

更新我的情况,这让我快要发疯了

我现在可以将 SQL 中的文本设置为我的通知,但它每次都只是我数据库中的第一个。现在我想出了两种可能的解决方案,它们只是理论上的。

  1. 有没有什么方法可以让每次调用数据库的通知都跳转到下一个结果?

  2. 正如我上面提到的,我正在设置一个闹钟,当闹钟响起时它会调用通知。现在,在我的警报未决意图中,我给它一个 ID,你可以看到 alarm_request_code 有没有办法将它提供给我的通知 ID,然后设置一个语句,如果通知 ID 等于或者在我的 SQL 上存储的警报 ID 列表中,然后它将搜索要输入的正确文本。

    MyDatabaseHandler myDatabaseHandler = new MyDatabaseHandler(context, null, null, 1);

    Cursor cursor = myDatabaseHandler.getAllProducts();
    
    
    // Information we are trying to acquire but can only get first result
    String name = cursor.getString(cursor.getColumnIndexOrThrow("alarm_name"));
    String date = cursor.getString(cursor.getColumnIndexOrThrow("alarm_date"));
    
    // Alarm ID FROM SQL which we want to match with the NOTIFICATION ID.....
    String alarm_request_code = cursor.getString(cursor.getColumnIndexOrThrow("alarm_code"));
    
    
    
    ////
    
    
    
    
    Log.i("The reciever is working", "perfect");
    
    //create an intent to service
    
    Intent service_Intent = new Intent(context, MessageService.class);
    context.startService(service_Intent);
    
    //Notification
    
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    
    
    
    Intent moveActivity = new Intent(context, AlarmActivity.class);
    
    
    moveActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    // Works with moveActivity to move the user back to main application.
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, moveActivity, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setContentTitle(name)
            .setContentText(date)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true);
    
    
    
    
    notificationManager.notify(Integer.valueOf(alarm_request_code), builder.build());
    
    
    
    }
    }
    

更新文本存储 mBuilder 变量 并稍后以这种方式更新:

mBuilder.setContentTitle(head); mBuilder.setContentText(body); mNotificationManager.notify(SIMPLE_ID, mBuilder.build());

编辑

    private void simple() {

        Intent intent = new Intent(context, AlarmActivity.class);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle(getResources().getString(R.string.app_name))
                .setContentText(getResources().getString(R.string.app_name))
                .setContentIntent(pIntent);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            mBuilder.setSmallIcon(R.drawable.ic_done_24dp);
        } else {
            mBuilder.setSmallIcon(someiconLollipop);
            mBuilder.setColor(somecolorLollipop]);
        }

        noti = mBuilder.build();
//use flags if you need:
        noti.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
    }

更新文本:

      private void showText() {
            String head = yournewhead;
String body = yournewbody;

            mBuilder.setContentTitle(head);
            mBuilder.setContentText(body);

            mNotificationManager.notify(SIMPLE_ID, mBuilder.build());
        }

根据文档,您可以执行以下操作:

mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")
    .setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user

    mNotifyBuilder.setContentText(currentText)
        .setNumber(++numMessages);
    // Because the ID remains unchanged, the existing notification is
    // updated.
    mNotificationManager.notify(
            notifyID,
            mNotifyBuilder.build());

这里的技巧是您的通知 ID,它必须相同才能正常运行,在您的情况下为 0。

所以只要它没有被解雇,它就应该相应地工作。我会建议将唯一 ID 作为自定义通知的常量,例如 123。

 notificationManager.notify(0, builder.build());