Android的Notification相关设置
Android手机:三星Galaxy S6
Android版本:Android 7.0
Android系统自带的本地通知会从顶部Pop下来,用来提示用户有新的消息,然后在Notification栏中停留。
Android接入远程推送后,并不会默认Pop出Notification,所以有时候用户很难发现自己收到了远程推送的消息,这款三星手机就是这样。
截图如下:
三星手机默认的Notification,显示标题,显示单行文本。但是为啥人家网易新闻的推送就有多行Text,而且别人右侧还显示一个大图标。。。
终于,最终还是在一个论坛中找到了答案。看截图:
重点来了,有没有代码,当然有:
创建多行文本的Notification
- //发送通知
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext())
- //设置可以显示多行文本
- .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
- .setContentTitle(title)
- .setContentText(text)
- .setSmallIcon(R.drawable.appicon)
- //设置大图标
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("悬浮通知")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND );
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = notifyBuilder.build();
- mNotifyMgr.notify( notificationID, notification);
监听Notification的Click事件和Cancel事件。
step1:分别设置notifyBuilder的 setContentIntent 和 setDeleteIntent。注意创建PendingIntent的时候,需要传递一个requestCode,这个requestCode每个Notification需要各不相同,否则只能监听一个Notification的Click和Cancel事件,后创建的Notification会由于相同的requestCode而覆盖之前的Notification,导致监听的函数永远只执行一次。
- //创建点击Notification的通知
- Intent intentClick = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- intentClick.setAction("notification_clicked");
- intentClick.putExtra(MyNotificationReceiver.TYPE, notificationID);
- PendingIntent pendingIntentClick = PendingIntent.getBroadcast(RichApplication.getContext(), notificationID, intentClick, PendingIntent.FLAG_UPDATE_CURRENT);
- //创建取消Notification的通知
- Intent intentCancel = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- intentCancel.setAction("notification_cancelled");
- intentCancel.putExtra(MyNotificationReceiver.TYPE, notificationID);
- PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(RichApplication.getContext(), notificationID, intentCancel, PendingIntent.FLAG_UPDATE_CURRENT);
- //发送通知
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext())
- //设置可以显示多行文本
- .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
- .setContentTitle(title)
- .setContentText(text)
- .setSmallIcon(R.drawable.appicon)
- //设置大图标
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("悬浮通知")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND )
- //设置点击Notification的监听事件
- .setContentIntent(pendingIntentClick)
- //设置CancelNotification的监听事件
- .setDeleteIntent(pendingIntentCancel);
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = notifyBuilder.build();
- mNotifyMgr.notify( notificationID, notification);
step2:新建一个Class继承BroadcastReceiver。
- public class MyNotificationReceiver extends BroadcastReceiver {
- static String TAG = "MyNotification";
- public static final String TYPE = "type";
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- int notificationId = intent.getIntExtra(TYPE, -1);
- Log.i(TAG, "传递过来的Notification的ID是:"+notificationId);
- Log.i(TAG, "当前收到的Action:"+action);
- if (action.equals("notification_clicked")) {
- //处理点击事件
- MyLocalNotificationManager.removeLocalNotification(notificationId);
- Log.i(TAG, "点击了notification");
- //跳转到App的主页
- Intent mainIntent = new Intent(context, MainActivity.class);
- mainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
- context.startActivity(mainIntent);
- }
- if (action.equals("notification_cancelled")) {
- //处理滑动清除和点击删除事件
- MyLocalNotificationManager.removeLocalNotification(notificationId);
- Log.i(TAG, "取消了notification");
- }
- }
- }
step3:在AndroidManifest.xml文件中注册receiver
- <!-- 监听Notification的点击和消失事件 -->
- <receiver android:name="com.richapp.home.MyNotificationReceiver">
- <intent-filter>
- <action android:name="notification_cancelled"/>
- <action android:name="notification_clicked"/>
- </intent-filter>
- </receiver>
设置App右上角的角标
由于Android的手机类型很多,而原本的安卓系统没有提供角标的功能,所以各大厂商自己定制了角标。我测试过三星手机和红米手机,三星手机的角标可以自由设置,即使启动App,角标还是可以继续存在,很类似iOS。但是红米手机开启App之后,角标就会消失,即使你不想它消失。
- public class MyLocalNotificationManager {
- private static String TAG = "MyLocalNTManager";
- /**
- * 添加一个新的Notification
- * @param title
- * @param text
- * @param notificationID
- */
- public static void addLocalNotification(String title, String text, int notificationID){
- int badgeNumber = 0;
- Log.i(TAG, "添加一个新的通知:"+notificationID);
- //获取当前Notification的数量
- Object notificationCount = SharedPreferenceUtils.get(RichApplication.getContext(), "NotificationCount", 0);
- int nCount = 0;
- if (notificationCount != null){
- nCount = Integer.parseInt(notificationCount.toString());
- }
- Log.i(TAG, "获取的本地通知的个数:"+nCount);
- //获取即时通讯数据库的数量
- List<Contact> contactList = ContactDbHelper.getInstance(RichApplication.getContext()).getAllShowContact();
- int sum = 0;
- for (Contact unreadCountact : contactList){
- sum += unreadCountact.unReadCount;
- }
- Log.i(TAG, "获取未读消息的个数:"+sum);
- //在当前未读消息的总数上+1
- badgeNumber = sum + nCount + 1;
- //创建点击Notification的通知
- Intent intentClick = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- intentClick.setAction("notification_clicked");
- intentClick.putExtra(MyNotificationReceiver.TYPE, notificationID);
- PendingIntent pendingIntentClick = PendingIntent.getBroadcast(RichApplication.getContext(), notificationID, intentClick, PendingIntent.FLAG_UPDATE_CURRENT);
- //创建取消Notification的通知
- Intent intentCancel = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- intentCancel.setAction("notification_cancelled");
- intentCancel.putExtra(MyNotificationReceiver.TYPE, notificationID);
- PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(RichApplication.getContext(), notificationID, intentCancel, PendingIntent.FLAG_UPDATE_CURRENT);
- //发送通知
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext())
- //设置可以显示多行文本
- .setStyle(new NotificationCompat.BigTextStyle().bigText(text))
- .setContentTitle(title)
- .setContentText(text)
- .setSmallIcon(R.drawable.appicon)
- //设置大图标
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("悬浮通知")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND )
- //设置点击Notification
- .setContentIntent(pendingIntentClick)
- .setDeleteIntent(pendingIntentCancel);
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = notifyBuilder.build();
- //设置右上角的角标数量
- if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")){
- //小米手机
- boolean isMiUIV6 = true;
- Class miuiNotificationClass = null;
- try {
- //递增
- badgeNumber = 1;
- Field field = notification.getClass().getDeclaredField("extraNotification");
- Object extraNotification = field.get(notification);
- Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
- method.invoke(extraNotification, badgeNumber);
- } catch (Exception e) {
- e.printStackTrace();
- //miui 6之前的版本
- isMiUIV6 = false;
- Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
- localIntent.putExtra("android.intent.extra.update_application_component_name",
- RichApplication.getContext().getPackageName()
- + "/"+ "com.richapp.home.MainActivity" );
- localIntent.putExtra("android.intent.extra.update_application_message_text",badgeNumber);
- RichApplication.getContext().sendBroadcast(localIntent);
- }
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZUK")){
- //联想手机
- final Uri CONTENT_URI = Uri.parse("content://com.android.badge/badge");
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", badgeNumber);
- RichApplication.getContext().getContentResolver().call(CONTENT_URI, "setAppBadgeCount", null, extra);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")){
- //OPPO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("com.oppo.unsettledevent");
- badgeIntent.putExtra("pakeageName", componentName.getPackageName());
- badgeIntent.putExtra("number", badgeNumber);
- badgeIntent.putExtra("upgradeNumber", badgeNumber);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("VIVO")){
- //VIVO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
- badgeIntent.putExtra("packageName", componentName.getPackageName());
- badgeIntent.putExtra("className", componentName.getClassName());
- badgeIntent.putExtra("notificationNum", badgeNumber);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZTE")){
- //中兴手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", badgeNumber);
- extra.putString("app_badge_component_name", componentName.flattenToString());
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- RichApplication.getContext().getContentResolver().call(
- Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"),
- "setAppUnreadCount", null, extra);
- }
- }
- else{
- //三星,华为等其他都是广播
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
- badgeIntent.putExtra("badge_count", badgeNumber);
- badgeIntent.putExtra("badge_count_package_name", componentName.getPackageName());
- badgeIntent.putExtra("badge_count_class_name", componentName.getClassName());
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- mNotifyMgr.notify( notificationID, notification);
- //更改SharedPrefrence中存储的数据
- SharedPreferenceUtils.put(RichApplication.getContext(), "NotificationCount", new Integer(nCount+1));
- Log.i(TAG, "更新本地通知的数量:"+(nCount+1));
- }
- /**
- * 移除一个Notification
- * @param notificationID
- */
- public static void removeLocalNotification(int notificationID){
- int badgeNumber = 0;
- Log.i(TAG, "移除一个新的通知:"+notificationID);
- //获取当前Notification的数量
- Object notificationCount = SharedPreferenceUtils.get(RichApplication.getContext(), "NotificationCount", 0);
- int nCount = 0;
- if (notificationCount != null){
- nCount = Integer.parseInt(notificationCount.toString());
- }
- Log.i(TAG, "获取的本地通知的个数:"+nCount);
- //获取即时通讯数据库的数量
- List<Contact> contactList = ContactDbHelper.getInstance(RichApplication.getContext()).getAllShowContact();
- int sum = 0;
- for (Contact unreadCountact : contactList){
- sum += unreadCountact.unReadCount;
- }
- Log.i(TAG, "获取未读消息的个数:"+sum);
- //在当前未读消息的总数上+1
- badgeNumber = sum + nCount - 1;
- // //创建点击Notification的通知
- // Intent intentClick = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- // intentClick.setAction("notification_clicked");
- // intentClick.putExtra(MyNotificationReceiver.TYPE, notificationID);
- // PendingIntent pendingIntentClick = PendingIntent.getBroadcast(RichApplication.getContext(), 0, intentClick, PendingIntent.FLAG_ONE_SHOT);
- //
- // //创建取消Notification的通知
- // Intent intentCancel = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- // intentCancel.setAction("notification_cancelled");
- // intentCancel.putExtra(MyNotificationReceiver.TYPE, notificationID);
- // PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(RichApplication.getContext(), 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);
- //发送通知
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext())
- .setStyle(new NotificationCompat.BigTextStyle().bigText(""))
- .setContentTitle("")
- .setContentText("")
- .setSmallIcon(R.drawable.appicon)
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("悬浮通知")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND );
- Intent intent = new Intent(RichApplication.getContext(), MainActivity.class);
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = notifyBuilder.build();
- //移除当前的Notification
- mNotifyMgr.cancel(notificationID);
- //更改SharedPrefrence中存储的数据
- SharedPreferenceUtils.put(RichApplication.getContext(), "NotificationCount", new Integer(nCount-1));
- Log.i(TAG, "更新本地通知的数量:"+(nCount-1));
- //设置右上角的角标数量
- if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")){
- //小米手机
- boolean isMiUIV6 = true;
- Class miuiNotificationClass = null;
- try {
- badgeNumber = -1;
- Field field = notification.getClass().getDeclaredField("extraNotification");
- Object extraNotification = field.get(notification);
- Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
- method.invoke(extraNotification, badgeNumber);
- } catch (Exception e) {
- e.printStackTrace();
- //miui 6之前的版本
- isMiUIV6 = false;
- Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
- localIntent.putExtra("android.intent.extra.update_application_component_name",
- RichApplication.getContext().getPackageName()
- + "/"+ "com.richapp.home.MainActivity" );
- localIntent.putExtra("android.intent.extra.update_application_message_text",badgeNumber);
- RichApplication.getContext().sendBroadcast(localIntent);
- }
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZUK")){
- //联想手机
- final Uri CONTENT_URI = Uri.parse("content://com.android.badge/badge");
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", badgeNumber);
- RichApplication.getContext().getContentResolver().call(CONTENT_URI, "setAppBadgeCount", null, extra);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")){
- //OPPO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("com.oppo.unsettledevent");
- badgeIntent.putExtra("pakeageName", componentName.getPackageName());
- badgeIntent.putExtra("number", badgeNumber);
- badgeIntent.putExtra("upgradeNumber", badgeNumber);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("VIVO")){
- //VIVO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
- badgeIntent.putExtra("packageName", componentName.getPackageName());
- badgeIntent.putExtra("className", componentName.getClassName());
- badgeIntent.putExtra("notificationNum", badgeNumber);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZTE")){
- //中兴手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", badgeNumber);
- extra.putString("app_badge_component_name", componentName.flattenToString());
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- RichApplication.getContext().getContentResolver().call(
- Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"),
- "setAppUnreadCount", null, extra);
- }
- }
- else{
- //三星,华为等其他都是广播
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
- badgeIntent.putExtra("badge_count", badgeNumber);
- badgeIntent.putExtra("badge_count_package_name", componentName.getPackageName());
- badgeIntent.putExtra("badge_count_class_name", componentName.getClassName());
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- // mNotifyMgr.notify( notificationID, notification);
- }
- /**
- * 移除全部Notification
- */
- public static void removeAllLocalNotification(){
- int badgeNumber = 0;
- Log.i(TAG, "移除全部的通知");
- //获取当前Notification的数量
- Object notificationCount = SharedPreferenceUtils.get(RichApplication.getContext(), "NotificationCount", 0);
- int nCount = 0;
- if (notificationCount != null){
- nCount = Integer.parseInt(notificationCount.toString());
- }
- Log.i(TAG, "获取的本地通知的个数:"+nCount);
- //获取即时通讯数据库的数量
- List<Contact> contactList = ContactDbHelper.getInstance(RichApplication.getContext()).getAllShowContact();
- int sum = 0;
- for (Contact unreadCountact : contactList){
- sum += unreadCountact.unReadCount;
- }
- //在当前未读消息的总数上+1
- badgeNumber = 0;
- // //创建点击Notification的通知
- // Intent intentClick = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- // intentClick.setAction("notification_clicked");
- // intentClick.putExtra(MyNotificationReceiver.TYPE, notificationID);
- // PendingIntent pendingIntentClick = PendingIntent.getBroadcast(RichApplication.getContext(), 0, intentClick, PendingIntent.FLAG_ONE_SHOT);
- //
- // //创建取消Notification的通知
- // Intent intentCancel = new Intent(RichApplication.getContext(), MyNotificationReceiver.class);
- // intentCancel.setAction("notification_cancelled");
- // intentCancel.putExtra(MyNotificationReceiver.TYPE, notificationID);
- // PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(RichApplication.getContext(), 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);
- //发送通知
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext())
- .setStyle(new NotificationCompat.BigTextStyle().bigText(""))
- .setContentTitle("")
- .setContentText("")
- .setSmallIcon(R.drawable.appicon)
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("悬浮通知")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND );
- //Intent intent = new Intent(RichApplication.getContext(), MainActivity.class);
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = notifyBuilder.build();
- //设置右上角的角标数量
- if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")){
- //小米手机
- boolean isMiUIV6 = true;
- Class miuiNotificationClass = null;
- try {
- Field field = notification.getClass().getDeclaredField("extraNotification");
- Object extraNotification = field.get(notification);
- Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
- method.invoke(extraNotification, badgeNumber);
- } catch (Exception e) {
- e.printStackTrace();
- //miui 6之前的版本
- isMiUIV6 = false;
- Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
- localIntent.putExtra("android.intent.extra.update_application_component_name",
- RichApplication.getContext().getPackageName()
- + "/"+ "com.richapp.home.MainActivity" );
- localIntent.putExtra("android.intent.extra.update_application_message_text",badgeNumber);
- RichApplication.getContext().sendBroadcast(localIntent);
- }
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZUK")){
- //联想手机
- final Uri CONTENT_URI = Uri.parse("content://com.android.badge/badge");
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", badgeNumber);
- RichApplication.getContext().getContentResolver().call(CONTENT_URI, "setAppBadgeCount", null, extra);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")){
- //OPPO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("com.oppo.unsettledevent");
- badgeIntent.putExtra("pakeageName", componentName.getPackageName());
- badgeIntent.putExtra("number", badgeNumber);
- badgeIntent.putExtra("upgradeNumber", badgeNumber);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("VIVO")){
- //VIVO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
- badgeIntent.putExtra("packageName", componentName.getPackageName());
- badgeIntent.putExtra("className", componentName.getClassName());
- badgeIntent.putExtra("notificationNum", badgeNumber);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZTE")){
- //中兴手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", badgeNumber);
- extra.putString("app_badge_component_name", componentName.flattenToString());
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- RichApplication.getContext().getContentResolver().call(
- Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"),
- "setAppUnreadCount", null, extra);
- }
- }
- else{
- //三星,华为等其他都是广播
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
- badgeIntent.putExtra("badge_count", badgeNumber);
- badgeIntent.putExtra("badge_count_package_name", componentName.getPackageName());
- badgeIntent.putExtra("badge_count_class_name", componentName.getClassName());
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- //更改SharedPrefrence中存储的数据
- SharedPreferenceUtils.put(RichApplication.getContext(), "NotificationCount", 0);
- Log.i(TAG, "更新本地通知的数量:"+0);
- //mNotifyMgr.notify( notificationID, notification);
- //清除所有的本地Notification
- mNotifyMgr.cancelAll();
- }
- /**
- * 读取即时通讯消息,更新角标
- */
- public static void updateNotificationAndBadgeNumber(){
- //清除通知栏信息
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- mNotifyMgr.cancel(R.drawable.appicon);
- //获取当前Notification的数量
- Object notificationCount = SharedPreferenceUtils.get(RichApplication.getContext(), "NotificationCount", 0);
- int nCount = 0;
- if (notificationCount != null){
- nCount = Integer.parseInt(notificationCount.toString());
- }
- Log.i(TAG, "获取的本地通知的个数:"+nCount);
- //更新角标显示
- //获取未读消息条数
- //获取当前数据库的值,然后设置未读消息数量
- List<Contact> contactList = ContactDbHelper.getInstance(RichApplication.getContext()).getAllShowContact();
- int sum = 0;
- for (Contact unreadCountact : contactList){
- sum += unreadCountact.unReadCount;
- }
- //计算全部的未读消息数量
- sum += nCount;
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext()).setContentTitle("")
- .setContentText("")
- .setSmallIcon(R.drawable.appicon)
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND );
- Intent intent = new Intent(RichApplication.getContext(), MainActivity.class);
- PendingIntent resultPendingIntent =
- PendingIntent.getActivity( RichApplication.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
- notifyBuilder.setContentIntent( resultPendingIntent );
- Notification notification = notifyBuilder.build();
- //设置右上角的角标数量
- if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")){
- //小米手机
- boolean isMiUIV6 = true;
- Class miuiNotificationClass = null;
- try {
- //Field field = notification.getClass().getDeclaredField("extraNotification");
- //Object extraNotification = field.get(notification);
- //Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
- //method.invoke(extraNotification, sum);
- //mNotifyMgr.notify( R.drawable.imcover, notification);
- } catch (Exception e) {
- e.printStackTrace();
- //miui 6之前的版本
- isMiUIV6 = false;
- Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
- localIntent.putExtra("android.intent.extra.update_application_component_name",
- RichApplication.getContext().getPackageName()
- + "/"+ "com.richapp.home.MainActivity" );
- localIntent.putExtra("android.intent.extra.update_application_message_text",sum);
- RichApplication.getContext().sendBroadcast(localIntent);
- }
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZUK")){
- //联想手机
- final Uri CONTENT_URI = Uri.parse("content://com.android.badge/badge");
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", sum);
- RichApplication.getContext().getContentResolver().call(CONTENT_URI, "setAppBadgeCount", null, extra);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")){
- //OPPO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("com.oppo.unsettledevent");
- badgeIntent.putExtra("pakeageName", componentName.getPackageName());
- badgeIntent.putExtra("number", sum);
- badgeIntent.putExtra("upgradeNumber", sum);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("VIVO")){
- //VIVO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
- badgeIntent.putExtra("packageName", componentName.getPackageName());
- badgeIntent.putExtra("className", componentName.getClassName());
- badgeIntent.putExtra("notificationNum", sum);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZTE")){
- //中兴手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", sum);
- extra.putString("app_badge_component_name", componentName.flattenToString());
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- RichApplication.getContext().getContentResolver().call(
- Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"),
- "setAppUnreadCount", null, extra);
- }
- }
- else{
- //三星,华为等其他都是广播
- Intent launchIntent = RichApplication.getContext().getPackageManager().getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
- badgeIntent.putExtra("badge_count", sum);
- badgeIntent.putExtra("badge_count_package_name", componentName.getPackageName());
- badgeIntent.putExtra("badge_count_class_name", componentName.getClassName());
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- }
- /**
- * 收到即时通讯消息,发出本地通知,并更新角标
- * @param subject msg的subject
- * @param msgBody msg的msgBody
- */
- public static void receivedNewMessageAndupdateBadgeNumber(String subject, String msgBody){
- //获取未读消息条数
- //获取当前数据库的值,然后设置未读消息数量
- List<Contact> contactList = ContactDbHelper.getInstance(RichApplication.getContext()).getAllShowContact();
- int sum = 0;
- for (Contact unreadCountact : contactList){
- sum += unreadCountact.unReadCount;
- }
- if (sum == 1){
- msgBody = "["+sum+" "+RichApplication.getContext().getResources().getString(R.string.IMUnReadMessage)+"]"+msgBody;
- }else{
- msgBody = "["+sum+" "+RichApplication.getContext().getResources().getString(R.string.IMUnReadMessages)+"]"+msgBody;
- }
- //获取当前Notification的数量
- Object notificationCount = SharedPreferenceUtils.get(RichApplication.getContext(), "NotificationCount", 0);
- int nCount = 0;
- if (notificationCount != null){
- nCount = Integer.parseInt(notificationCount.toString());
- }
- //Log.i(TAG, "获取的本地通知的个数:"+nCount);
- //计算整体的
- sum += nCount;
- NotificationCompat.Builder notifyBuilder =
- new NotificationCompat.Builder(RichApplication.getContext()).setContentTitle(subject)
- .setContentText(msgBody)
- .setSmallIcon(R.drawable.appicon)
- .setLargeIcon(BitmapFactory.decodeResource(RichApplication.getContext().getResources(), R.drawable.appicon))
- // 点击消失
- .setAutoCancel(true)
- // 设置该通知优先级
- .setPriority(Notification.PRIORITY_MAX)
- .setTicker("悬浮通知")
- // 通知首次出现在通知栏,带上升动画效果的
- .setWhen(System.currentTimeMillis())
- // 通知产生的时间,会在通知信息里显示
- // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
- .setDefaults( Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND );
- Intent intent = new Intent(RichApplication.getContext(), MainActivity.class);
- PendingIntent resultPendingIntent =
- PendingIntent.getActivity( RichApplication.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
- notifyBuilder.setContentIntent( resultPendingIntent );
- NotificationManager mNotifyMgr = (NotificationManager) RichApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
- Notification notification = notifyBuilder.build();
- //设置右上角的角标数量
- if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")){
- //小米手机
- boolean isMiUIV6 = true;
- Class miuiNotificationClass = null;
- try {
- // Field field = notification.getClass().getDeclaredField("extraNotification");
- // Object extraNotification = field.get(notification);
- // Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);
- // method.invoke(extraNotification, sum);
- } catch (Exception e) {
- e.printStackTrace();
- //miui 6之前的版本
- isMiUIV6 = false;
- Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
- localIntent.putExtra("android.intent.extra.update_application_component_name",
- RichApplication.getContext().getPackageName()
- + "/"+ "com.richapp.home.MainActivity" );
- localIntent.putExtra("android.intent.extra.update_application_message_text",sum);
- RichApplication.getContext().sendBroadcast(localIntent);
- }
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZUK")){
- //联想手机
- final Uri CONTENT_URI = Uri.parse("content://com.android.badge/badge");
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", sum);
- RichApplication.getContext().getContentResolver().call(CONTENT_URI, "setAppBadgeCount", null, extra);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("OPPO")){
- //OPPO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("com.oppo.unsettledevent");
- badgeIntent.putExtra("pakeageName", componentName.getPackageName());
- badgeIntent.putExtra("number", sum);
- badgeIntent.putExtra("upgradeNumber", sum);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("VIVO")){
- //VIVO手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
- badgeIntent.putExtra("packageName", componentName.getPackageName());
- badgeIntent.putExtra("className", componentName.getClassName());
- badgeIntent.putExtra("notificationNum", sum);
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- else if (Build.MANUFACTURER.equalsIgnoreCase("ZTE")){
- //中兴手机
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Bundle extra = new Bundle();
- extra.putInt("app_badge_count", sum);
- extra.putString("app_badge_component_name", componentName.flattenToString());
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
- RichApplication.getContext().getContentResolver().call(
- Uri.parse("content://com.android.launcher3.cornermark.unreadbadge"),
- "setAppUnreadCount", null, extra);
- }
- }
- else{
- //三星,华为等其他都是广播
- Intent launchIntent = RichApplication.getContext().getPackageManager()
- .getLaunchIntentForPackage(RichApplication.getContext().getPackageName());
- ComponentName componentName = launchIntent.getComponent();
- Intent badgeIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
- badgeIntent.putExtra("badge_count", sum);
- badgeIntent.putExtra("badge_count_package_name", componentName.getPackageName());
- badgeIntent.putExtra("badge_count_class_name", componentName.getClassName());
- RichApplication.getContext().sendBroadcast(badgeIntent);
- }
- mNotifyMgr.notify( R.drawable.appicon, notification);
- }
- }
参考链接:
http://blog.csdn.net/bdmh/article/details/41804695
http://www.jianshu.com/p/20ad37d1418b
Android的Notification相关设置的更多相关文章
- Android studio界面相关设置
用惯了emacs的操作方式,每当使用一款新的编辑器的时候,第一个想到的就是这个工具有没有emacs的快捷键,Android studio也是一样的. 1. Android studio设置emacs的 ...
- 使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置
使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置 http://icaoye.com/virtualbox-run-android/
- Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API
想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...
- Android studio相关设置及实现存在于工程目录中的视频播放
一:相关设置 1:主题设置 File-->Settings-->Appearance &Behavior-->Appearance-->THeme 2:Java源码的颜 ...
- Android 添加源码到eclipse 以及相关设置
作者:舍得333 主页:http://blog.sina.com.cn/u/1509658847版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版.作者信息和本声明,否则将追究法律 ...
- Android之Notification介绍
Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)
在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...
- 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
随机推荐
- jquery获取页面iframe内容
//取得整个HTML格式 var f = $(window.frames["ReportIFrame"].document).contents().html(); 或者 $(&qu ...
- 【python】-- pymsql 操作MySQL
pymysql 对MySQL数据库进行简单数据操作python模块主要是:MySQLdb.pymsql,MySQLdb模块主要用于python2.X,而python3.X则使用pymsql,pymys ...
- 洛谷 P2486 [SDOI2011]染色
题目描述 输入输出格式 输入格式: 输出格式: 对于每个询问操作,输出一行答案. 输入输出样例 输入样例#1: 6 5 2 2 1 2 1 1 1 2 1 3 2 4 2 5 2 6 Q 3 5 C ...
- Nodejs课堂笔记-第三课 构建一个nodejs的Docker镜像
本文由Vikings(http://www.cnblogs.com/vikings-blog/) 原创,转载请标明.谢谢! 因为一直做Linux有关的开发工作,所以不习惯在Windows平台编译和测试 ...
- JVM性能优化, Part 1 ―― JVM简介
JVM性能优化这些列文章共分为5章,是ImportNew上面翻译自Javaworld: 第1章:JVM技术概览 第2章:编译器 第3章:垃圾回收 第4章:并发垃圾回收 第5章:可伸缩性 众所周知,Ja ...
- 每天一个Linux命令(28)df命令
报告文件系统磁盘空间的使用情况.获取硬盘被占用了多少空间,目前还剩下多少空间等信息. (1)用法: 用法: df [选项] [文件] (2)功能: 功能: 显示 ...
- mysql设置有外键的主键自增及其他
有外键的主键设置自增. ; ALTER TABLE `<table>` MODIFY COLUMN `id` ) NOT NULL AUTO_INCREMENT FIRST; 创建数据库, ...
- CKeditor插件开发流程(一)
1.放在多文件中 第一步:config.js中 config.extraPlugins = '插件名称';//注册插件,extraPlugins只允许出现一次,你如果之前有新增别的插件,那么用逗号分隔 ...
- [原创]Scala学习:流程控制,异常处理
1.流程控制 1)do..while def doWhile(){ var line="" do{ line = readLine() println("readline ...
- 【leetcode刷题笔记】Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...