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相关设置的更多相关文章

  1. Android studio界面相关设置

    用惯了emacs的操作方式,每当使用一款新的编辑器的时候,第一个想到的就是这个工具有没有emacs的快捷键,Android studio也是一样的. 1. Android studio设置emacs的 ...

  2. 使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置

    使用VIRTUALBOX安装ANDROID系统 | 图文教程 | 相关设置 http://icaoye.com/virtualbox-run-android/

  3. Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API

    想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...

  4. Android studio相关设置及实现存在于工程目录中的视频播放

    一:相关设置 1:主题设置 File-->Settings-->Appearance &Behavior-->Appearance-->THeme 2:Java源码的颜 ...

  5. Android 添加源码到eclipse 以及相关设置

    作者:舍得333 主页:http://blog.sina.com.cn/u/1509658847版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版.作者信息和本声明,否则将追究法律 ...

  6. Android之Notification介绍

    Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...

  7. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  8. Android 通知栏Notification的整合 全面学习 (一个DEMO让你全然了解它)

    在android的应用层中,涉及到非常多应用框架.比如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架.通知机制,ActionBar框架等等. ...

  9. 【转】 [置顶] Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在Android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

随机推荐

  1. lua例子getglobal()

    #include <stdio.h> #define MAX_COLOR 255 extern "C" { #include "lua-5.2.2/src/l ...

  2. 九度OJ 1207:质因数的个数 (质数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5939 解决:1926 题目描述: 求正整数N(N>1)的质因数的个数. 相同的质因数需要重复计算.如120=2*2*2*3*5,共有 ...

  3. ABAP服务器文件操作

    转自http://blog.itpub.net/547380/viewspace-876667/ 在程序设计开发过程中,很多要对文件进行操作,这又分为对本地文件操作和服务器文件操作.对本地文件操作使用 ...

  4. IDEA main方法自动补全(转发:http://blog.csdn.net/zjx86320/article/details/52684601)

    最近刚从Eclipse转到IDEA,各种学习丫,IDEA里的main方法是不能自动补齐的,肿么办呢? 1.首先,点击File-->Settings-->Editor-->Live T ...

  5. VS2013 Pro版本密钥

    Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY

  6. Java InetAddress.getByAddress()的使用

    import java.net.*; public class NetDemo { public static void main(String[] args) throws Exception{ S ...

  7. [原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. BootStrap实现左侧或右侧竖式tab选项卡

    BootStrap实现左侧或右侧竖式tab选项卡 代码如下: <div style="height: 100px;"> <div class="col- ...

  9. 20145229吴姗珊《Java程序设计》2天总结

    20145229吴姗珊<Java程序设计>2天总结 教材学习内容总结 异常处理 1.使用try.catch Java中所有错误都会被包装成对象,可以尝试(try)执行程序并捕捉(catch ...

  10. hbase shell概述

    hbase shell-general(常规指令):http://www.cnblogs.com/husky/p/6374867.html hbase shell-ddl(表定义指令):http:// ...