使用通知ID更改徽标和放大器文本 [英] Use notification ID to change logo & text

查看:124
本文介绍了使用通知ID更改徽标和放大器文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道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);
通知通知=新通知(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);
}


解决方案

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

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.

这篇关于使用通知ID更改徽标和放大器文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆