APP接收通知处理逻辑项目实战经验总结

说在前面:
关于Notification,我之前写过两篇博客,分别为Android之Notification详解Android O通知适配
今天,我们来对APP接收通知处理逻辑进行项目实战经验总结。

正文:
说到通知,我们能想到的最简单的场景:点击手机上接收到的通知,跳转到对应的详情页。对应的代码示例如下:

        Intent mainIntent = new Intent();
        mainIntent.setClass(this, OrderManagementActivity.class);
        mainIntent.putExtra("orderId", orderId);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification;
        if (Build.VERSION.SDK_INT >= 26) {
            String channelId = "order";
            String channelName = "订单消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance, notificationManager);
            notification = new Notification.Builder(this, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("收到一条待支付订单,查看详情")
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .build();
        } else {
            notification = new Notification.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentText("收到一条待支付订单,查看详情")
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .build();
        }
        notificationManager.notify(0, notification);
    @TargetApi(26)
    private void createNotificationChannel(String channelId, String channelName, int importance, NotificationManager notificationManager) {
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
        notificationManager.createNotificationChannel(notificationChannel);
    }

但是,这只是在理想化的情况下,真实的用户场景千变万化,千奇百态。举例如下:
1、(前提:APP设置为自启动)来了一条通知,对应的Intent我设置成跳转至对应的详情页,然后这时我将APP的进程手动kill掉,通知还在,此时点击通知还是跳转至对应的详情页,显然这是不合理的。此时,点击通知,应该是先启动APP。
2、来了一条通知,对应的Intent我设置成跳转至对应的详情页,然后这时我退出APP账号,通知还在,此时点击通知还是跳转至对应的详情页,显然这是不合理的。此时点击通知应该先跳转至登录界面。
3、(前提:APP设置为自启动)APP的进程被kill掉,然后来了多条通知,点击其中一条通知,启动APP,最终跳转至对应的
详情界面。此时,再点击其他通知,还是启动APP,并最终跳转至对应的详情界面,显然是不合理的。此时应该直接跳转至对应的详情界面。

造成以上问题的原因在于:我们把逻辑判断放在了创建通知的时候。因为如果把逻辑判断放在了创建通知的时候,当在创建通知之后点击通知之前的这段时间,对当初逻辑判断结果产生影响的条件发生了变化,那么将出现我们不想要的结果。

那么应该怎么做呢?有两种方案。
方案一:
弄一个路由Activity,即:点击通知都是首先跳转至这个路由Activity,在这个Activity中进行
相应地判断,然后进行相应地跳转。
但是有一个明显的缺点,就是要多一层Activity跳转,并且对用户体验的影响是很明显的。因为在
某种情况下是完全可以跳转至目标Activity的,而这里还要先经过一个路由Activity。并且我参考了
微信、微博、今日头条等APP,都没有采用这种方案。

方案二:
监听通知点击,并在点击通知的时候进行判断。
这里我们需要使用到BroadcastReceiver。
实现步骤如下:
1、创建一个NotificationClickReceiver,继承BroadcastReceiver。并在AndroidManifest中进行注册。
2、在创建通知的地方做如下改动(结合上述示例代码):

mainIntent.setClass(this, OrderManagementActivity.class);
改成
mainIntent.setClass(this, NotificationClickReceiver.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
改成
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

3、在NotificationClickReceiver中重写onReceive方法,在onReceive方法中进行逻辑判断。

猜你喜欢

转载自blog.csdn.net/zdj_Develop/article/details/88865264
今日推荐