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); } }
参考链接: