美文网首页
记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 极光推送接入工作

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