Android Notification使用

Android Notification使用

直接用得话使用最下面的工具类

  • 基于android.support环境,androidx环境参考官方demo,也可以自己进行改动。换下包名
  • 编译环境27、29都测试过,
  • 最低版本限制16
  • 悬浮窗在小米上测试过,需要手动开启悬浮窗权限
  • 文档待整理

参考文档:

https://blog.csdn.net/qq_32209403/article/details/79238786

https://www.jianshu.com/p/c5aa2ba09925

https://blog.csdn.net/guolin_blog/article/details/79854070

https://blog.csdn.net/LosingCarryJie/article/details/79357257

https://blog.csdn.net/qq_35644307/article/details/85304874

https://blog.csdn.net/guolin_blog/article/details/79854070

https://blog.csdn.net/tiankongcheng6/article/details/78183377

https://blog.csdn.net/xialong_927/article/details/80253523 (广播接受)

https://blog.csdn.net/weixin_38196407/article/details/89556023 ( 据郭霖大神解释, 国产手机默认只给微信、QQ这类程序开横幅(即提醒式通知),其余应用需要手动去设置里面选择打开横幅才会有

google示例代码:

https://developer.android.com/training/notify-user/custom-notification

google示例合集:

https://github.com/android/user-interface-samples

官方:http://developer.android.com/design/patterns/notifications.html

译文:http://adchs.github.io/patterns/notifications.html

使用教程 :http://developer.android.com/training/notify-user/index.html

开发文档 :http://developer.android.com/reference/android/app/Notification.html

可使用代码:

当用户开通允许悬浮通知的设置后,可以悬浮显示通知(5.0后有效)

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        Intent intent = new Intent(Main2Activity.this,ReceiveMailActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.drawable.logo);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂式通知");
        builder.setFullScreenIntent(pendingIntent, true);
        mNotificationManager.notify(2, builder.build());

国产手机默认只给微信、QQ这类程序开横幅(即提醒式通知),其余应用需要手动去设置里面选择打开横幅才会有

https://www.jianshu.com/p/20ad37d1418b
https://www.jianshu.com/p/97f9f20ab36d
PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                        build.context,
                        1,
                        notifyIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT//更新之前的消息,使用FLAG_CANCEL_CURRENT时应保持requestCode每次更新一次
                );
/** 通知工具类
	代码基于android.support环境
*/
public class NotificationUtil {

    public void showNotification(NotificationUtilBuild build) {
        if (null == build){
            throw new RuntimeException("NotificationUtilBuild 不能为空");
        }
        NotificationManager notificationManager =
                (NotificationManager) build.context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;//5.0以上会显示横幅
            //注意Name和description不能为null或者""
            NotificationChannel mChannel = new NotificationChannel(build.channelId, build.channelName, importance);
            // 配置通知渠道的属性
            mChannel.setDescription(build.description);
            notificationManager.createNotificationChannel(mChannel);
        }

        Intent notifyIntent = new Intent(build.context, MainActivity.class);
        PendingIntent notifyPendingIntent =
                PendingIntent.getActivity(
                        build.context,
                        1,
                        notifyIntent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );

        NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder( build.context, build.channelId);
//Notification.Builder builder = new Notification.Builder(this);//不要使用这个,否则,弹出的通知不显示时间        
        notificationCompatBuilder.setAutoCancel(true);
        notificationCompatBuilder.setContentTitle(build.title);
        notificationCompatBuilder.setContentText(build.content);
        notificationCompatBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationCompatBuilder.setContentIntent(notifyPendingIntent);
        notificationCompatBuilder.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH);
        notificationCompatBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
//		 builder.setColor()//这个没效果,待测试
//       builder.setColor(ContextCompat.getColor())//这个有效果        
 //     自定义View-------start       
//        RemoteViews notificationLayout = new RemoteViews(getPackageName(), //R.layout.notification_custom_layout);

//        notificationCompatBuilder.setStyle(new //NotificationCompat.DecoratedCustomViewStyle());//完全自定义的话去掉这句话
//        notificationCompatBuilder.setCustomContentView(notificationLayout);
//     自定义View-------end 
        Notification notification = notificationCompatBuilder.build();
        notificationManager.notify(build.notificationId, notification);
    }

    public NotificationUtilBuild createBuild(){
        return new NotificationUtilBuild();
    }

    /** 通知工具类的Build */
    public class NotificationUtilBuild{
        public int notificationId = 1;
        public String notificationTag = "chat";
        public Context context;
        public String title = "";
        public String content = "";
        public String time = "";
        public String channelId = "chat";
        public String channelName = "聊天";
        public String description = "聊天通知";
    }

}
发布了132 篇原创文章 · 获赞 29 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/Mr_Tony/article/details/103252208