安卓手機集成廠商通道后,在極光后臺測試發(fā)送消息

image.png
通知下發(fā)策略選擇僅通過廠商通道下發(fā),app在后臺就可以收到消息。但是,通過廠商通道發(fā)送的消息,在JPushMessageReceiver的onNotifyMessageArrived和onNotifyMessageOpened方法中無法實現(xiàn)對消息的監(jiān)聽
@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 點擊通知欄跳轉(zhuǎn)頁面
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
繼續(xù)往下看

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";
/**該通知的下發(fā)通道**/
private static final String KEY_WHICH_PUSH_SDK = "rom_type";
/**通知標(biāo)題**/
private static final String KEY_TITLE = "n_title";
/**通知內(nèi)容**/
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();
}
/**
* 處理點擊事件,當(dāng)前啟動配置的Activity都是使用
* Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
* 方式啟動,只需要在onCreat中調(diào)用此方法進(jìn)行處理
*/
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>
再去極光后臺發(fā)送通過廠商通道推送的消息,在通知欄點擊這條通知,handleOpenClick方法就會監(jiān)聽到這條消息,自己就可以處理點擊事件了。