使用通知ID更改标志和文字(Use notification ID to change logo &

2019-10-29 19:44发布

我想知道这取决于内容,如Facebook应用程序如何改变他们的通知标题和标志。

例如,在Facebook上,如果你得到标记你得到另一个标题及其他标识。

我认为它应该是可能的通知创造了独特的通知。 虽然我无法找到这方面的任何明显的例子。

我GenerateNotification:

private static void generateNotification(Context context, String message, String url) {
    int icon = R.drawable.ic_launcher;

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
    context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, ShowChange.class);
    notificationIntent.putExtra ("url",url);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);       
}

Answer 1:

它应该是这样的:(这基本上是与一些修改代码)

公共静态无效generateNotification(上下文上下文中,消息字符串,字符串的URL,INT icon_from_drawable){INT图标= icon_from_drawable;

long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

String title = context.getString(R.string.app_name);

Intent notificationIntent = new Intent(context, ShowChange.class);
notificationIntent.putExtra ("url",url);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
        PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;

//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);      

}

如果您对API级别的定位您的应用程序> = 11,然后用Notification.Builder参考这里: http://developer.android.com/reference/android/app/Notification.Builder.html还有一点,上面的代码将不断更新相同的通知对象,因为通知管理的通知方法正在总是相同的ID,即0,所以通知的同一对象将被更新。



文章来源: Use notification ID to change logo & text