1、在注册清单中加入
<receiver android:name="your receiver全路径" android:enabled="true"> <intent-filter> <!-- 以下是要添加的权限 --> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_CLICK_ACTION" /> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="your包名" /> </intent-filter> </receiver>
2、编写receiver
package com.youzheng.slqx.activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import java.util.Iterator; import java.util.logging.Logger; import cn.jpush.android.api.JPushInterface; /** * 自定义接收器 * * 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */ public class MyTReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID); //send the Registration Id to your server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { //用户点击通知会走的方法 //获取推送消息的方法 String content = bundle.getString(JPushInterface.EXTRA_ALERT); // 在这里可以自己写代码去定义用户点击后的行为 if(context != null){ Log.e("content",content); //例如 如果推送内容以【消息】开头 则点击后跳转到消息的Activity 否则跳转到主页面 if(content.startsWith("【气象预警】")||content.startsWith("【水利预警】")){//判断内容的条件 Intent i = new Intent(context, YJJCActivity.class); //打开消息界面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } if(content.startsWith("【国突预警】")){ Intent i = new Intent(context, YJTXActivity.class); //打开主界面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } Intent i = new Intent(context, YJJKActivity.class); //打开主界面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); }else{ } } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等.. } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) { boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false); } else { } } catch (Exception e){ } }
3、极光有一个bug就是必须展开通知点击才能进入指定的页面。????
4、还有一个注意的地方,极光推送的内容,不包含标题的,切记切记,可尝试打印内容,通过内容的特定文字进行判断。