美文网首页
安卓极光推送监听厂商通道的消息

安卓极光推送监听厂商通道的消息

作者: 小欢_J | 来源:发表于2021-06-18 13:56 被阅读0次

    安卓手机集成厂商通道后,在极光后台测试发送消息


    image.png

    通知下发策略选择仅通过厂商通道下发,app在后台就可以收到消息。但是,通过厂商通道发送的消息,在JPushMessageReceiver的onNotifyMessageArrived和onNotifyMessageOpened方法中无法实现对消息的监听

     @Override
        public void onNotifyMessageArrived(Context context, NotificationMessage notificationMessage) {
            super.onNotifyMessageArrived(context, notificationMessage);
            Log.e(TAG,"onNotifyMessageArrived" + notificationMessage.toString() );
        }
    
        @Override
        public void onNotifyMessageOpened(Context context, NotificationMessage notificationMessage) {
            super.onNotifyMessageOpened(context, notificationMessage);
            //todo 点击通知栏跳转页面
            Intent intent = new Intent(context, FriendChattingActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            Log.e(TAG,"onNotifyMessageOpened" + notificationMessage.toString());
        }
    

    并没有打印log。
    去看了一下极光文档,说了下面一段话


    image.png

    继续往下看


    image.png
    于是去极光后台添加了一个属性
    image.png
    然后在你的项目新建一个OpenClickActivity
    public class OpenClickActivity extends Activity {
        private static final String TAG = "OpenClickActivity";
        /**消息Id**/
        private static final String KEY_MSGID = "msg_id";
        /**该通知的下发通道**/
        private static final String KEY_WHICH_PUSH_SDK = "rom_type";
        /**通知标题**/
        private static final String KEY_TITLE = "n_title";
        /**通知内容**/
        private static final String KEY_CONTENT = "n_content";
        /**通知附加字段**/
        private static final String KEY_EXTRAS = "n_extras";
        private TextView mTextView;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mTextView = new TextView(this);
            setContentView(mTextView);
            handleOpenClick();
        }
    
        /**
         * 处理点击事件,当前启动配置的Activity都是使用
         * Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
         * 方式启动,只需要在onCreat中调用此方法进行处理
         */
        private void handleOpenClick() {
            Log.d(TAG, "用户点击打开了通知");
            Toast.makeText(this,"用户点击打开了通知",Toast.LENGTH_SHORT);
            String data = null;
            //获取华为平台附带的jpush信息
            if (getIntent().getData() != null) {
                data = getIntent().getData().toString();
            }
    
            //获取fcm、oppo、vivo、华硕、小米平台附带的jpush信息
            if (TextUtils.isEmpty(data) && getIntent().getExtras() != null) {
                data = getIntent().getExtras().getString("JMessageExtra");
            }
    
            Log.w(TAG, "msg content is " + String.valueOf(data));
            if (TextUtils.isEmpty(data)) return;
            try {
                JSONObject jsonObject = new JSONObject(data);
                String msgId = jsonObject.optString(KEY_MSGID);
                byte whichPushSDK = (byte) jsonObject.optInt(KEY_WHICH_PUSH_SDK);
                String title = jsonObject.optString(KEY_TITLE);
                String content = jsonObject.optString(KEY_CONTENT);
                String extras = jsonObject.optString(KEY_EXTRAS);
                StringBuilder sb = new StringBuilder();
                sb.append("msgId:");
                sb.append(String.valueOf(msgId));
                sb.append("\n");
                sb.append("title:");
                sb.append(String.valueOf(title));
                sb.append("\n");
                sb.append("content:");
                sb.append(String.valueOf(content));
                sb.append("\n");
                sb.append("extras:");
                sb.append(String.valueOf(extras));
                sb.append("\n");
                sb.append("platform:");
                sb.append(getPushSDKName(whichPushSDK));
                mTextView.setText(sb.toString());
    
                //上报点击事件
                JPushInterface.reportNotificationOpened(this, msgId, whichPushSDK, data);
            } catch (JSONException e) {
                Log.w(TAG, "parse notification error");
            }
        }
    
        private String getPushSDKName(byte whichPushSDK) {
            String name;
            switch (whichPushSDK) {
                case 0:
                    name = "jpush";
                    break;
                case 1:
                    name = "xiaomi";
                    break;
                case 2:
                    name = "huawei";
                    break;
                case 3:
                    name = "meizu";
                    break;
                case 4:
                    name = "oppo";
                    break;
                case 5:
                    name = "vivo";
                    break;
                case 6:
                    name = "asus";
                    break;
                case 8:
                    name = "fcm";
                    break;
                default:
                    name = "jpush";
            }
            return name;
        }
    
    }
    

    然后在项目的AndroidManifest.xml配置

    <activity android:name="com.xxx.jpush.OpenClickActivity"
              android:exported="true">
              <intent-filter>
                  <action android:name="android.intent.action.VIEW" />
                  <category android:name="android.intent.category.DEFAULT" />
                  <action android:name="com.xxx.jpush.OpenClickActivity" />
              </intent-filter>
          </activity>
    

    再去极光后台发送通过厂商通道推送的消息,在通知栏点击这条通知,handleOpenClick方法就会监听到这条消息,自己就可以处理点击事件了。

    参考OPPO厂商通道集成指南

    相关文章

      网友评论

          本文标题:安卓极光推送监听厂商通道的消息

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