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. docker安装并配置加速

    安装 旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本: sudo apt-get remove docker \ docker-engine \ ...

  2. 九度OJ 1254:N皇后问题 (N皇后问题、递归、回溯)

    时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:765 解决:218 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一 ...

  3. SQL2000 3核6核 CUP 安装SP4

    1.找到HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432node\Microsoft\MSSQLServer \MSSQLServer\Parameters\ 2.然后加入下面的 ...

  4. 【ELK】抓取AWS-ELB日志的logstash配置文件

    前言 ELK搭建没有难度,难的是logstash的配置文件,logstash主要分为三个部分,input,filter和output. input,输入源可选的输入源由很多,详情见ELK官网,这里我们 ...

  5. HDU - 1175 连连看 【DFS】【BFS】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1175 思路 这种题一想到就用搜索, 但是内存是32m 用 bfs 会不会MLE 没错 第一次 BFS的 ...

  6. 每天一个Linux命令(4)touch命令

    touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来:二是用来创建新的空文件.     (1)用法 用法:touch [选项]... ...

  7. 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题

    题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码

  8. jQuery水平滑动菜单

    在线演示 本地下载

  9. Delphi 运行Word VBA 宏 删除软回车

    Sub 整理网页()'整理网页:删除软回车.删除空白段.使段落文字两端对齐Selection.WholeStory        Selection.Find.ClearFormatting    S ...

  10. 纯CSS3实现的动感菜单效果

    1. [代码] 纯CSS3实现的动感菜单效果 <!DOCTYPE html><head><meta http-equiv="Content-Type" ...