美文网首页
Notification 通知使用

Notification 通知使用

作者: that_is_this | 来源:发表于2018-07-11 11:03 被阅读23次

    1. 通知的使用

    在 Android 8.0 后,通知需要添加 channel ,所以需要添加版本区别

        public void showNotifictionIcon(Context context) {
            try {
                NotificationCompat.Builder builder = null;
                NotificationManager manager = null;
                int Notification_id = 1;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(String.valueOf(Notification_id), "bweakeup", NotificationManager.IMPORTANCE_DEFAULT);
                    channel.enableLights(true); //是否在桌面icon右上角展示小红点
                    channel.setLightColor(Color.GREEN); //小红点颜色
                    channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
                    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    manager.createNotificationChannel(channel);
                    builder = new NotificationCompat.Builder(context, "" + Notification_id);
                }else{
                    builder = new NotificationCompat.Builder(context);
                    manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                }
                Log.i("Wooo", "showNotifictionIcon in");
    
                Intent intent = new Intent(context, NotActivity.class);//只显示通知,无页面跳转
                builder.setAutoCancel(true);//点击后消失
                builder.setSmallIcon(R.drawable.app_icon);//设置通知栏消息标题的头像
                builder.setDefaults(NotificationCompat.DEFAULT_SOUND);//设置通知铃声
                builder.setTicker("状态栏显示的文字");
                builder.setContentTitle("新消息...");
                long cTime = System.currentTimeMillis();
                builder.setContentText("当前时间 :" + cTime);
                //利用PendingIntent来包装我们的intent对象,使其延迟跳转
                PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                builder.setContentIntent(intentPend);
    
    
                manager.notify(0x1234, builder.build());
                Log.i("Wooo", "showNotifictionIcon out ");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    2. 通知成功与否的监听

    1. AndroidManifest.xml 添加 service
            <service android:name=".abstest.NLService"
                android:label="通知测试"
                android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
                <intent-filter>
                    <action android:name="android.service.notification.NotificationListenerService"/>
                </intent-filter>
            </service>
    
    1. 编写 NLService 类
    @RequiresApi(Build.VERSION_CODES.KITKAT)
    public class NLService extends NotificationListenerService {
        @Override
        public void onNotificationPosted(StatusBarNotification sbn) {
            super.onNotificationPosted(sbn);
            String pkgName = sbn.getPackageName();
            Context ctx = this.getApplicationContext();
            Log.i("Wooo", "onNotificationPosted in : " + pkgName);
            if (pkgName.equals("***.***.***")) {
                Log.i("Wooo", "onNotificationPosted send success");
            }
        }
    
        @Override
        public void onNotificationRemoved(StatusBarNotification sbn) {
            super.onNotificationRemoved(sbn);
            Log.i("Wooo", "onNotificationRemoved in");
        }
    }
    
    1. 权限申请

    在应用启动时请求通知监听权限

            if (!notificationListenerEnable()) {
                gotoNotificationAccessSetting(this.getApplicationContext());
            }
    
        private boolean notificationListenerEnable() {
            Log.i("Wooo", "notificationListenerEnable in");
            boolean enable = false;
            String packageName = getPackageName();
            String flat= Settings.Secure.getString(getContentResolver(),"enabled_notification_listeners");
            if (flat != null) {
                enable= flat.contains(packageName);
            }
            return enable;
        }
    
        private boolean gotoNotificationAccessSetting(Context context) {
            try {
                Log.i("Wooo", "gotoNotificationAccessSetting in");
                Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                return true;
            } catch(ActivityNotFoundException e) {
                try {
                    Intent intent = new Intent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings$NotificationAccessSettingsActivity");
                    intent.setComponent(cn);
                    intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
                    context.startActivity(intent);
                    return true;
                } catch(Exception ex) {
                    ex.printStackTrace();
                }
                return false;
            }
        }
    

    这样,在允许了权限后,应用发送通知,当成功后, Listener 就会产生回调。

    相关文章

      网友评论

          本文标题:Notification 通知使用

          本文链接:https://www.haomeiwen.com/subject/dszcpftx.html