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); } }
參考鏈接: