極光推送整合注意事項

在將極光推送整合項目中的過程中,有如下事項需要注意:
1:極光Demo中libs下面的so 及 jar 並 add as library;
極光Demo中的jpush_style.xml文件需要存放到自己項目中,裏面有所需要的style;
在清單配置文件添加權限和activity時,要留意位於權限上的極光自定義權限的添加。

    <permission
        android:name="你應用的包名.permission.JPUSH_MESSAGE"
        android:protectionLevel="signature" />

2:添加權限後,將極光Demo中的MyReceiver文件copy到自己項目中並按自己的需求修改後,需要將清單配置文件中原Demo的Receiver路徑,修改成這個自定義的MyReceiver。

        <!-- User defined.  For test only  用戶自定義的廣播接收器-->
        <receiver
            android:name="com.example.jpushdemo.MyReceiver"
            android:exported="false"
            android:enabled="true">
            <intent-filter>
                <action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required  用戶註冊SDK的intent-->
                <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <!--Required  用戶接收SDK消息的intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <!--Required  用戶接收SDK通知欄信息的intent-->
                <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!--Required  用戶打開自定義通知欄的intent-->
                <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!--Optional 用戶接受Rich Push Javascript 回調函數的intent-->
                <action android:name="cn.jpush.android.intent.CONNECTION" /><!-- 接收網絡變化 連接/斷開 since 1.6.3 -->
                <category android:name="com.yuu.upost_c" />
            </intent-filter>
        </receiver>

3:MyReceiver中,點擊通知後跳轉的Activity,需修改成自己要進入顯示的Activity。

            Log.d(TAG, "[MyReceiver] 用戶點擊打開了通知");

            //打開自定義的Activity
            Intent i = new Intent(context, TestActivity.class);
            i.putExtras(bundle);
            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
            context.startActivity(i);

4:MyReceiver中,收到的推送信息要傳給自己定義的Activity,並在接受信息的Activity中對接受的消息做處理。
MyReceiver中將接受的消息傳出到自己的Activity

    //send msg to MainActivity
    //MyReceiver中將接受的消息傳出到自己的Activity
    private void processCustomMessage(Context context, Bundle bundle) {
        if (MainActivity.isForeground) {
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
            msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
            if (!ExampleUtil.isEmpty(extras)) {
                try {
                    JSONObject extraJson = new JSONObject(extras);
                    if (null != extraJson && extraJson.length() > 0) {
                        msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
                    }
                } catch (JSONException e) {

                }

            }
            context.sendBroadcast(msgIntent);
        }
    }

接受消息的Activity對消息進行處理

//for receive customer msg from jpush server
//接受消息的Activity對消息進行處理
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
              String messge = intent.getStringExtra(KEY_MESSAGE);
              String extras = intent.getStringExtra(KEY_EXTRAS);
              StringBuilder showMsg = new StringBuilder();
              showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
              if (!ExampleUtil.isEmpty(extras)) {
                  showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
              }
              setCostomMsg(showMsg.toString());
            }
        }
    }

    private void setCostomMsg(String msg){
         if (null != msgText) {
             msgText.setText(msg);
             msgText.setVisibility(android.view.View.VISIBLE);
         }
    }

接受消息的Activity在寫好處理完消息的方案後,要記得註冊廣播。

    //接受消息的Activity創建時就註冊廣播
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();   
        registerMessageReceiver();  // used for receive msg
    }

在不需要再廣播的時候註銷掉廣播。一般在主Activity銷燬後註銷,也可以自己在恰當的地方註銷,比如接受消息的Activity銷燬後,若不再需要處理消息了,也可以註銷掉廣播。

    @Override
    protected void onDestroy() {
        unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

5:在APP進入的第一個activity中,其生命週期要調用極光推送的生命週期,使得兩者的生命週期一致。
這第一個activity一般就是歡迎界面的activity,若沒有設置歡迎界面,一般就是主activity。
這個步驟若沒有,極光推送會有通知彈出來提示。

    @Override
    protected void onResume() {
        //極光推送的啓動
        JPushInterface.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        //極光推送的暫停
        JPushInterface.onPause(this);
        super.onPause();
    }

6:要記得在Application中初始化極光推送。

         JPushInterface.setDebugMode(true);     // 設置開啓日誌,發佈時請關閉日誌
         JPushInterface.init(this);             // 初始化 JPush
         JPushInterface.setLatestNotificationNumber(this, 5);//設置最近保留的通知條數

7:極光推送的官方Demo中,包含一些默認使用的資源文件,如圖片、佈局等,其中有一張圖片“jpush_notification_icon”,該圖片用於極光推送展示通知的佈局,默認情況下使用該圖片做展示。
當需要使用自己的特定圖標做展示時,可以自定義通知欄樣式:

BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(MainActivity.this);
builder.statusBarDrawable = R.drawable.jpush_notification_icon;
builder.notificationFlags = Notification.FLAG_AUTO_CANCEL
        | Notification.FLAG_SHOW_LIGHTS;  //設置爲自動消失和呼吸燈閃爍
builder.notificationDefaults = Notification.DEFAULT_SOUND
        | Notification.DEFAULT_VIBRATE
        | Notification.DEFAULT_LIGHTS;  // 設置爲鈴聲、震動、呼吸燈閃爍都要
JPushInterface.setPushNotificationBuilder(1, builder);
//這是是新增編號爲1的通知樣式,需要在通知發送時,對樣式的“通知欄樣式編號”進行選定;如果要取代官方的默認樣式,則要調用
//JPushInterface.setDefaultPushNotificationBuilder(builder);該方法會替換官方默認的通知樣式;可在初始化JPushInterface.init(this)成功後的任意地方調用

如果直接使用極光官方的通知樣式,而又要使用自己的圖標,則可直接刪除該“jpush_notification_icon”圖片,則默認的通知樣式會使用application中的android:icon=”@drawable/icon”做展示。

8:如果要使用自定義的聲音取代原來的通知鈴聲,則先將builder.notificationDefaults方法中的鈴聲去除,再可通過SoundPool來播放鈴聲。

builder.notificationDefaults = Notification.DEFAULT_VIBRATE;//只設置震動
    //這裏SoundPool是static,需要在剛開始就初始化,如果在onReceive方法中再初始化,則程序第一次啓動時,
    //第一次接受到的通知則沒有聲音,因爲soundId還沒有被正確賦值。除第一次後則正常。
    private static SoundPool sound = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    private static int soundId;
    //初始化SoundPool和soundId
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (sound != null) {
            soundId = sound.load(context, R.raw.mysong, 1);
        }
}

//當通知到來時,播放自定義鈴聲
if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下來的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下來的通知的ID: " + notifactionId);
            //調用自定義的鈴聲
            sound.play(soundId, 1f, 1f, 1, 0, 1f);

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