融雲--IMKit 自帶消息推送

2.6.0 之後的版本必須自定義一個繼承 PushMessageReceiver 的廣播接收器,否則可能會導致點擊後臺通知沒有反應,或者收不到推送通知等問題。

1、自定義一個 BroadcastReceiver 類

繼承PushMessageReceiver 類,實現onNotificationMessageArrived和onNotificationMessageClicked方法。

public class DemoNotificationReceiver extends PushMessageReceiver {
   /**
     * 用來接收服務器發來的通知欄消息(消息到達客戶端時觸發)
     * 默認return false,通知消息會以融雲 SDK 的默認形式展現
     * 如果需要自定義通知欄的展示,在這裏實現⾃己的通知欄展現代碼,只要return true即可
     */
    @Override
    public boolean onNotificationMessageArrived(Context context, PushNotificationMessage message) {

        return false;
    }
     /**
     * ⽤戶點擊通知欄消息時觸發 (注意:如果⾃定義了通知欄的展現,則不會觸發)
     * 默認 return false
     * 如果需要自定義點擊通知時的跳轉,return true即可
     */
    @Override
    public boolean onNotificationMessageClicked(Context context, PushNotificationMessage message) {
        return false;
    }
}

2、把DemoNotificationReceiver 註冊到應用的 AndroidManifest.xml 裏面

<receiver
    android:exported="true"
    android:name="您自定義的 broadcastReceiver 類名">
    <intent-filter>
        <action android:name="io.rong.push.intent.MESSAGE_ARRIVED" />
        <action android:name="io.rong.push.intent.MI_MESSAGE_ARRIVED" />
        <action android:name="io.rong.push.intent.MESSAGE_CLICKED" />
        <action android:name="io.rong.push.intent.MI_MESSAGE_CLICKED" />
    </intent-filter>
</receiver>

3、攔截推送消息的點擊

點擊推送消息時默認會觸發出下面的action 事件:

Intent intent = new Intent();
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);

Uri.Builder uriBuilder = Uri.parse("rong://" + this.getPackageName()).buildUpon();
uriBuilder.appendPath("push_message")
        .appendQueryParameter("targetId", targetId)
        .appendQueryParameter("pushData", pushData)
        .appendQueryParameter("pushId", pushId)
        .appendQueryParameter("extra", extra);

startActivity(intent);

這時我們可以在你的 AndroidManifest.xml ⾥面配置了 A activity 攔截這個 action, 那麼點擊時就會跳轉到 activity A。

<activity
android:name="A"
android:launchMode="singleTask"
android:screenOrientation="portrait">

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />

    <data
        android:host="你的包名"
        android:pathPrefix="/push_message"
        android:scheme="rong" />
</intent-filter>
</activity>

詳細內容請點擊:Android 推送服務開發指南

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章