Android8.0通知在极光推送基础上的修改

 2018年二月的第一天,我又开始装逼啦大笑,Android8.0出来已经有一段时间了,前段时间拿8.0的系统运行了一下自己的APP发现推送收不到,瞬间尴尬尴尬,查找了开发者文档发现Google大佬们对8.0的通知有了“哒哒滴”改善,今天就拿我解决推送的方法和大家分享一下,灰常滴简单,因为项目中以前的推送使用的是第三方极光推送,也不知道现在极光有木有支持8.0,呵呵,很大的原因是我觉得再重新集成新的jar包麻烦,也就没有去关注这个有木有更新。接下来要开始我的表演啦!微笑

         首先奉上官方文档的链接:点这里

public class JPushBtsReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("DDZTAG","收到广播了"+Build.VERSION.SDK_INT);
        Bundle bundle = intent.getExtras();
        //加一个推送管理类
        if(Build.VERSION.SDK_INT >=26){
            Log.e("DDZTAG","8.0处理了");
            NotificationManager notificationManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            String id=context.getPackageName();
            CharSequence name=context.getString(R.string.app_name);
            int importance=NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel=new NotificationChannel(id,name,importance);
            channel.enableLights(true);
            channel.setLightColor(Color.RED);
            channel.enableVibration(true);
            channel.setShowBadge(true);
            channel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});
            notificationManager.createNotificationChannel(channel);
            int notificationId = 0x1234;
            Notification.Builder builder = new Notification.Builder(context,id); //与channelId对应
            //icon title text必须包含,不然影响桌面图标小红点的展示
            String message=bundle.getString(JPushInterface.EXTRA_ALERT);
            
            builder.setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("消息的title")
                    .setContentText(message);
            builder.setAutoCancel(true);
            String FunctionId = "";
            String ParamId = "";
            String MsgId = "";
            String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
            try {
                JSONObject job = new JSONObject(json);
                FunctionId = job.optString("");
                ParamId = job.optString("");
                MsgId = job.optString("");

                Intent i = DDZConsts.getAimIntent(context,Integer.valueOf(FunctionId),ParamId,MsgId);
                if(MsgId != null && MsgId.length() > 0){
                    i.putExtra(DDZConsts.INTENT_EXTRA_MSG_ID,MsgId);
                }
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
                builder.setContentIntent(pendingIntent);
                notificationManager.notify(Integer.valueOf(FunctionId), builder.build());
            } catch (Exception e) {
                e.printStackTrace();
                Log.i("DDZTAG", "极光推送出错:" + e.getMessage());
            }

        }else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
        
            String FunctionId = "";
            String ParamId = "";
            String MsgId = "";
            String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
            try {
                JSONObject job = new JSONObject(json);
                FunctionId = job.optString("");
                ParamId = job.optString("");
                MsgId = job.optString("");
                

                Intent i = DDZConsts.getAimIntent(context,Integer.valueOf(FunctionId),ParamId,MsgId);
                if(MsgId != null && MsgId.length() > 0){
                    i.putExtra(DDZConsts.INTENT_EXTRA_MSG_ID,MsgId);
                }
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            } catch (Exception e) {
                e.printStackTrace();
                Log.i("DDZTAG", "极光推送出错:" + e.getMessage());
            }

        }
    }


}

 有木有很简单,就是简单的区分一下手机的版本是不是8.0或者更高版本,如果是就进8.0的处理逻辑,否则还用以前的处理逻辑。这样也算是一种偷懒对不偷笑

 其中的一些逻辑,也非常的简单,官方文档也有很详细的说明,在这我就不再用我那拙劣的文笔描述了,如果有哪些地方不明白可以在评论区评论出来,大家一起交流,互相提高。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值