使用通知ID更改徽标和文本

时间:2013-05-19 17:19:00

标签: java android push-notification google-cloud-messaging

我想知道像Facebook这样的应用程序如何改变他们的通知标题&徽标取决于内容。

例如,在Facebook中,如果您被标记,则会获得另一个标题&另一个标志。

我认为应该可以通过notify创建一个唯一的通知。 虽然我找不到任何明确的例子。

我的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);       
}

1 个答案:

答案 0 :(得分:0)

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

public static void generateNotification(Context context,String message,String url,int icon_from_drawable){     int icon = 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 LEVEL> = 11定位您的应用,请使用Notification.Builder 请参考:http://developer.android.com/reference/android/app/Notification.Builder.html 还有一点,上面的代码将继续更新相同的通知对象,因为通知管理器的通知方法始终采用相同的id,即0,因此通知的同一对象将被更新。