问题描述
我想知道像Facebook应用程序如何改变他们的通知标题和放大器;根据内容的标志。
例如,在Facebook中,如果你得到标记你得到另一个标题和放大器;另一个标志。
我想它应该是可能的通知创造了独特的通知。
虽然我找不到任何明显的例子这一点。
我GenerateNotification:
私有静态无效generateNotification(上下文的背景下,消息字符串,字符串URL){
INT图标= R.drawable.ic_launcher; 时长= System.currentTimeMillis的();
NotificationManager notificationManager =(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新的通知(图标,消息,时); 字符串title = context.getString(R.string.app_name); 意图notificationIntent =新意图(背景下,ShowChange.class);
notificationIntent.putExtra(URL,URL);
//设置的意图,因此它不会启动新活动
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
的PendingIntent意图=
PendingIntent.getActivity(上下文,0,notificationIntent,0);
notification.setLatestEventInfo(上下文,标题,邮件,意图);
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,通知);}
应该是这样的:(这基本上是你的code有一些修改)
公共静态无效generateNotification(上下文的背景下,消息字符串,字符串的URL,诠释icon_from_drawable){
INT图标= icon_from_drawable;
时长= System.currentTimeMillis的();
NotificationManager notificationManager =(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新的通知(图标,消息,时);字符串title = context.getString(R.string.app_name);意图notificationIntent =新意图(背景下,ShowChange.class);
notificationIntent.putExtra(URL,URL);
//设置的意图,因此它不会启动新活动
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
的PendingIntent意图=
PendingIntent.getActivity(上下文,0,notificationIntent,0);
notification.setLatestEventInfo(上下文,标题,邮件,意图);
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,通知);
}
如果您对API级别的定位您的应用程序> = 11,然后用Notification.Builder
这里指:
还有一点上面code会不断更新相同的通知对象,因为通知管理的通知方法正在总是相同的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.Builderrefer here : http://developer.android.com/reference/android/app/Notification.Builder.htmlOne 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来更改标识和放大器的Andriod;文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!