且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-16 15:30:52

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

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);
通知通知=新通知(icon,message,when);

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

Intent notificationIntent = new Intent(context,ShowChange.class);
notificationIntent.putExtra(url,url);
//设置意图,因此它不会启动新的活动
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;

//播放默认通知声音
notification.defaults | = Notification.DEFAULT_SOUND;

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

//振动如果启用振动
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,所以相同的通知对象将被更新。


I'd like to know how applications like Facebook change their notification title & logo depending on the content.

For example, in Facebook, if you get tagged you get another title & another logo.

I assume it should be possible with notify to create a unique notification. Though I can't find any clear examples for this.

My 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);       
}

It should be something like this:(this is basically your code with some modifications)

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

}

If you targeting your app for API LEVEL>=11, then use Notification.Builder refer here : http://developer.android.com/reference/android/app/Notification.Builder.html One more point the above code will keep updating the same notification object because the notify method of notification manager is taking always the same id i.e 0, so the same object of notification will be updated.