美文网首页
记Android 极光推送接入工作

记Android 极光推送接入工作

作者: taoqx | 来源:发表于2019-10-16 16:26 被阅读0次

1、JCenter接入

依赖jpush和jcore模块,开启厂商通道需依赖对应模块lib,并在极光后台对应的应用设置中开启厂商开关。

dependencies {
    implementation 'cn.jiguang.sdk:jpush:3.3.6'
    implementation 'cn.jiguang.sdk:jcore:2.1.4'
    api 'cn.jiguang.sdk.plugin:xiaomi:3.3.6'
    api 'cn.jiguang.sdk.plugin:huawei:3.3.6'
    api 'cn.jiguang.sdk.plugin:meizu:3.3.6'
}

AndroidManifest中配置权限,添加必要组件。

        <!-- Since JCore2.0.0 Required SDK核心功能-->
        <!-- 可配置android:process参数将Service放在其他进程中;android:enabled属性不能是false -->
        <!-- 这个是自定义Service,要继承极光JCommonService,可以在更多手机平台上使得推送通道保持的更稳定 -->
        <service android:name=".PushService"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="cn.jiguang.user.service.action" />
            </intent-filter>
        </service>


        <receiver android:name=".JPushCustomMessageReceiver">
            <intent-filter android:priority="1000">
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
                <!--Required  显示通知栏 -->
                <category android:name="${applicationId}" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
            <!-- Optional -->
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

JPushCustomMessageReceiver用户自定义广播接受者,继承JPushMessageReceiver,在onMessage方法中用户可以自己处理消息回调。

    /**
     * 收到自定义消息回调
     *
     * @param context       Application
     * @param customMessage Message
     */
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        Log.i("jiguang", customMessage.extra);

        HandleMessage.handleCustomMessage(context, customMessage);
    }

最后在代码中调用极光初始化Api。

2、厂商通道

通过集成厂商通道,即使应用退出进程也可以收到推送消息,需要单独去各厂商注册应用,申请id、key等值。
module的build.gradle中需要定义厂商渠道变量。

            manifestPlaceholders = ["authoritiesID"  : applicationId,
                                    XIAOMI_APPKEY    : "MI-您的应用对应的小米的APPKEY",//小米平台注册的appkey
                                    XIAOMI_APPID     : "MI-您的应用对应的小米的APPID",//小米平台注册的appid
                                    HUAWEI_APPID     : "您的应用对应华为的appID",
                                    MEIZU_APPKEY     : "MZ-您的应用对应的魅族appkey",//魅族平台注册的appkey
                                    MEIZU_APPID      : "MZ-您的应用对应魅族的appid",//魅族平台注册的appid
            ]

注意小米、魅族的前缀,因为小米给的AppId是一个长数字型字符,最终打包后的值会被gradle替换为一个错误值。

3、推送消息

在极光后台有两只推送方式:通知和自定义消息;而服务端则是通过Api推送。
在极光后台推送时,可选择通过channel、标签或者registeredId来精准推送。

4、极光推送自定义铃声

通过极光后台的自定义消息方式推送,在JPushCustomMessageReceiver的onMessage回调中,创建自定义通知。
Android 26开始,创建通知需要使用NotificationChannel,在NotificationManager#createNotificationChannel方法调用前,可通过NotificationChannel#setSound设置自定义铃声。
此通道的名称也将显示在手机通知管理中,默认铃声即为设置的自定义铃声。

    // 自定义消息的通知使用此channel,自定义铃声
    public static String CUSTOM_CHANNEL_ID = "custom_channel_id";
    public static String CUSTOM_CHANNEL_NAME = "自定义推送";

    /**
     * 创建通知渠道
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private static void notificationChannel() {

        NotificationManager manager = (NotificationManager) ZBApplication.getApplication().getSystemService(NOTIFICATION_SERVICE);

        NotificationChannel channel = new NotificationChannel(CUSTOM_CHANNEL_ID,
                CUSTOM_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

        Uri sound = Uri.parse("android.resource://" + ZBApplication.getApplication().getPackageName() + "/"+ R.raw.sound);
        channel.setSound(sound, Notification.AUDIO_ATTRIBUTES_DEFAULT);

        channel.enableLights(true);//显示桌面红点
        channel.setLightColor(Color.GREEN);
        channel.enableVibration(true);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        manager.createNotificationChannel(channel);

    }

如果发现自定义铃声未生效,应当排查此channel的id是否在其他地方已经注册过了。

相关文章

  • 记Android 极光推送接入工作

    1、JCenter接入 依赖jpush和jcore模块,开启厂商通道需依赖对应模块lib,并在极光后台对应的应用设...

  • 极光推送接入

    极光推送调研 极光接入 主要功能使用 总结 极光接入 申请极光AppKey 极光接入步骤/引入jar包 申请极光A...

  • 接入极光推送

    正常情况下,接入我们接入极光推送的sdk,当应用退出后,用户便无法再接受到推送,那么这样显示是不符合我们的需求得,...

  • Android极光推送(接入华为厂商)

    华为开发者联盟[https://developer.huawei.com/consumer/cn/] 登录华为开发...

  • Android极光推送(接入Oppo厂商)

    oppo开放平台[https://open.oppomobile.com/] 登录OPPO开放平台,依次选择“应用...

  • Netty-Apns接入实现

    极光推送免费版每分钟600次的请求限制实在是把我恶心坏了,考虑到现在我们 Android 的推送已经全量接入了小米...

  • iOS 接入极光推送

    一、推送证书的制作 ①创建一个推送的APPID 1.选择identifiers -> app ids 2.填写描述...

  • Android极光推送(接入小米厂商)

    小米开放平台[https://dev.mi.com/console/] 登录小米开放平台账号,点击应用服务-消息推...

  • android集成华为push 6003错误,以及华为低版本cr

    前段时间项目需要接入推送功能,最终对比选择了使用极光推送,但是极光结束后台进程是收不到push消息的,需要我们自己...

  • 极光推送的几个问题

    极光推送的几个问题 最近接入极光推送遇到了几个问题 服务端发出通知没有声音 需要设置声音字段的值, sound:'...

网友评论

      本文标题:记Android 极光推送接入工作

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