极光推送整合注意事项

在将极光推送整合项目中的过程中,有如下事项需要注意:
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);

        } 

你可能感兴趣的:(极光推送整合注意事项)