如何更新Android中的现有通知?

osi*_*the 4 android push-notification

我正在使用以下代码来显示通知。显示通知后,如果单击通知,我将进入活动。

void Notify(String notificationTitle, String notificationMessage){
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.noti, notificationTitle,System.currentTimeMillis());

        Intent notificationIntent = new Intent(this, SmsActivity.class);

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(this, notificationTitle,notificationMessage, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
       } 
Run Code Online (Sandbox Code Playgroud)

现在,我想更新通知文本。我怎样才能做到这一点 ?

Aks*_*hay 6

你可以试试这个

    //First time    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                        .setContentText(context.getString(R.string.notif_text))
                        .setContentTitle(title)
                        .setSmallIcon(R.drawable.ic_action_alarm_2)
                        .setAutoCancel(false)
                        .setOngoing(running)
                        .setOnlyAlertOnce(true)
                        .setContentIntent(
                                PendingIntent.getActivity(context, 10, 
                                        new Intent(context, YourActivity.class)                                 
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
                                0)
                        )

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

     //Second time

            builder.setContentTitle(title);
            notificationManager.notify(id, builder.build());
Run Code Online (Sandbox Code Playgroud)


Kis*_*oid 2

您可以通过在方法中发送不同的文本来更新通知文本 Notify(String notificationTitle, String notificationMessage)