是否可以更改 android 中重复通知的内容文本或消息?如果是,如何?

Is it possible to change the content text or message of a repeating notification in android ? If yes, how?

我在广播接收器中收到重复通知。我想动态替换内容文本。通知将在下次显示通知时向用户显示不同的消息。我想知道是否可能。如果是,如何?

下面是我的广播接收器class

public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
long pattern[] = {500, 500};
private Uri notifsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
private NotificationCompat.BigTextStyle contentStyle;

    @Override
    public void onReceive(Context context, Intent intent) {
    String msg = "Drivers who sit higher feel as if they're driving slower. " +
            "Thus, SUV drivers, who are already piloting the vehicles most prone to " +
            "roll, drive faster because they feel like they're creeping along. " +
            "So lower your seat to get the sensation of more speed.";


    contentStyle = new android.support.v4.app.NotificationCompat.BigTextStyle();
    contentStyle.bigText(msg);
    contentStyle.setBigContentTitle("Lower Your Seat");
    contentStyle.setSummaryText("AutoKit");

    NotificationCompat.Builder builder;
    builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("AutoKit")
            .setContentText("Tip of the Day")
            .setTicker("Daily Tip")
            .setStyle(contentStyle)
            .setSound(notifsound)
            .setAutoCancel(true)
            .setVibrate(pattern);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(1, builder.build());
    } 

}

这是在我的主活动中调用的方法,并将广播接收器绑定到警报管理器

public void setRepeatingAlarm(){ //user receives notifications every 24 hours at 7am
    am = (AlarmManager)this.getSystemService(this.ALARM_SERVICE);
    Intent intent = new Intent(this, TimeAlarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 07);
    calendar.set(Calendar.MINUTE, 00);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pi);
}

修改通知

要设置通知以便更新,请通过调用 NotificationManager.notify(ID, notification) 向其发出通知 ID。要在发出通知后更新此通知,请更新或创建一个 NotificationCompat.Builder 对象,从中构建一个通知对象,然后使用您之前使用的相同 ID 发出通知。

以下代码片段演示了更新后的通知以反映已发生事件的数量。它堆叠通知,显示摘要:

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());

...

取自开发者网站。请参考,http://developer.android.com/training/notify-user/managing.html

这里是我在广播接收器中所做的更改 class。

public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
long pattern[] = {500, 500};
private Uri notifsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
private NotificationCompat.BigTextStyle contentStyle;
private List contentTexts, contentTitles;

@Override
public void onReceive(Context context, Intent intent) {
    contentTexts = new ArrayList<String>();
    contentTitles = new ArrayList<String>();
    prepareContentTitles();
    prepareContentTexts();

    SharedPreferences prefs = context.getSharedPreferences("notification_count", context.MODE_PRIVATE);
    int count = prefs.getInt("notification_count", 0);

    contentStyle = new android.support.v4.app.NotificationCompat.BigTextStyle();
    contentStyle.bigText((CharSequence) contentTexts.get(count));
    contentStyle.setBigContentTitle((CharSequence) contentTitles.get(count));
    contentStyle.setSummaryText("AutoKit");

    NotificationCompat.Builder builder;
    builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("AutoKit")
            .setContentText("Tip of the Day")
            .setTicker("Daily Tip")
            .setStyle(contentStyle)
            .setSound(notifsound)
            .setAutoCancel(true)
            .setVibrate(pattern);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(1, builder.build());



    if (count == contentTexts.size() - 1) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("notification_count", 0);
        editor.commit();
    }
    else {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("notification_count", count + 1);
        editor.commit();
    }
}
public void prepareContentTexts() {
    contentTexts.add("Drivers who sit higher feel as if they're driving slower. " +
            "Thus, SUV drivers, who are already piloting the vehicles most prone to " +
            "roll, drive faster because they feel like they're creeping along. " +
            "So lower your seat to get the sensation of more speed.");
    contentTexts.add("Manufacturers recommend replacing your blades every three months. " +
            "Keep a spare set in your trunk. A product such as Rain Clear can also help " +
            "minimize the work of your wipers; spray it onto the glass every few weeks. " +
            "In some light rains, it makes the wipers almost unnecessary");
    contentTexts.add("At the BMW Performance Driving School, instructor Jim Clark says " +
            "these four words over and over: \"Slow in, fast out.\" When taking a corner," +
            " you need to scrub as much of that speed as you can while the car is braking" +
            " in a straight line, then you can accelerate out of the curve. The converse " +
            "is \"Fast in, maybe no out.\"");
}
public void prepareContentTitles() {
    contentTitles.add("Lower Your Seat");
    contentTitles.add("Rainproof Your Windshield");
    contentTitles.add("Maneuver Tight Corners ");
}

}

通知每次触发时显示不同的内容文本