美文网首页
移动推送 Android : Android 8.0 以上设备通

移动推送 Android : Android 8.0 以上设备通

作者: 方_f666 | 来源:发表于2019-12-10 18:06 被阅读0次

1. 问题

在 Android 8.0 以上的设备集成推送 SDK 后,推送接收不到,日志显示通知已经从服务端发送到客户端,但是并未创建通知,这是怎么回事?应该如何解决?

2. 问题原因

自 8.0 (API Level 26)起,Android 推出了NotificationChannel机制,旨在对通知进行分类管理。如果用户App的targetSdkVersion大于等于26,且并未设置NotificaitonChannel,创建的通知是不会弹出的。

3. 解决方案

阿里云移动推送自v3.1.1版本开始支持NotificationChannel机制,以下是接入步骤:

3.1 集成新版

  • 集成移动推送SDK v3.1.1及其以上版本

  • 集成服务端OpenApi SDK v3.9.0及其以上版本:

    <pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; padding: 10px; margin: 0px 0px 25px; overflow: auto; font: 14px/1.45 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; background: rgb(247, 247, 247); overflow-wrap: break-word; white-space: pre-wrap;">

    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>aliyun-java-sdk-push</artifactId>
    4. <version>3.9.0</version>
    5. </dependency>

    </pre>

3.2 注册NotificationChannel

在客户端创建自己的NotificationChannel,可参考下面代码

具体调用位置为:Application的onCreate,云推初始化前后都可以,可参考 Demo

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; padding: 10px; margin: 0px 0px 25px; overflow: auto; font: 14px/1.45 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; background: rgb(247, 247, 247); overflow-wrap: break-word; white-space: pre-wrap;">

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  2. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  3. // 通知渠道的id
  4. String id = "1";
  5. // 用户可以看到的通知渠道的名字.
  6. CharSequence name = "notification channel";
  7. // 用户可以看到的通知渠道的描述
  8. String description = "notification description";
  9. int importance = NotificationManager.IMPORTANCE_HIGH;
  10. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  11. // 配置通知渠道的属性
  12. mChannel.setDescription(description);
  13. // 设置通知出现时的闪灯(如果 android 设备支持的话)
  14. mChannel.enableLights(true);
  15. mChannel.setLightColor(Color.RED);
  16. // 设置通知出现时的震动(如果 android 设备支持的话)
  17. mChannel.enableVibration(true);
  18. mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  19. //最后在notificationmanager中创建该通知渠道
  20. mNotificationManager.createNotificationChannel(mChannel);
  21. }

</pre>

3.3 利用OpenApi推送

服务端推送时指定其NotificationChannelid,可参考如下代码:

<pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; padding: 10px; margin: 0px 0px 25px; overflow: auto; font: 14px/1.45 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; background: rgb(247, 247, 247); overflow-wrap: break-word; white-space: pre-wrap;">

  1. @Test

  2. public void testAdvancedPush() throws Exception {

  3. PushRequest pushRequest = new PushRequest();

  4. // 推送目标

  5. pushRequest.setAppKey(appKey);

  6. pushRequest.setTarget("DEVICE"); //推送目标: device:推送给设备; account:推送给指定帐号,tag:推送给自定义标签; all: 推送给全部

  7. pushRequest.setTargetValue("xxxxxxxxxxxxxxx");

  8. pushRequest.setPushType("NOTICE"); // 消息类型 MESSAGE NOTICE

  9. pushRequest.setDeviceType("ANDROID"); // 设备类型 ANDROID iOS ALL.

  10. // 推送配置

  11. pushRequest.setTitle("ALi Push Title"); // 消息的标题

  12. pushRequest.setBody("Ali Push Body"); // 消息的内容

  13. // 推送配置: Android

  14. pushRequest.setAndroidNotifyType("BOTH");//通知的提醒方式 "VIBRATE" : 震动 "SOUND" : 声音 "BOTH" : 声音和震动 NONE : 静音

  15. pushRequest.setAndroidOpenType("APPLICATION"); //点击通知后动作 "APPLICATION" : 打开应用 "ACTIVITY" : 打开AndroidActivity "URL" : 打开URL "NONE" : 无跳转

  16. // 指定notificaitonchannel id

  17. pushRequest.setAndroidNotificationChannel("1");

  18. ......

  19. }

</pre>

注:指定NotificationChannel后,通知的提醒方式(震动、声音等)均为NotificationChannel所指定的提醒方式,服务端的提醒方式配置不再生效

3.4 利用阿里云控制台推送

使用阿里云控制台推送通知时,需要设置 “高级设置”,在最下面的 “Android8.0 特殊配置:” ,填写 “通知通道:”,也就是上述客户端注册的 NotificationChannel 的 id,如下图所示:

image

相关文章

网友评论

      本文标题:移动推送 Android : Android 8.0 以上设备通

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