1、清单文件配置权限和界面
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.enjoydecorate" />
</intent-filter>
</activity>
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false"></service>
<service
android:name="cn.jpush.android.service.PushService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER" />
<action android:name="cn.jpush.android.intent.REPORT" />
<action android:name="cn.jpush.android.intent.PushService" />
<action android:name="cn.jpush.android.intent.PUSH_TIME" />
</intent-filter>
</service>
<service
android:name="cn.jpush.android.service.DaemonService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.DaemonService" />
<category android:name="com.enjoydecorate" />
</intent-filter>
</service>
<receiver
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true"
android:exported="false">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<category android:name="com.enjoydecorate" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<receiver android:name="cn.jpush.android.service.AlarmReceiver" />
<receiver
android:name=".receiver.JPushReceiver"
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.ACTION_RICHPUSH_CALLBACK" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="com.enjoydecorate" />
</intent-filter>
</receiver>
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="008a484eabfcd176edde4ed5" />
<activity android:name=".activity.PayActivity"></activity>
2.创建一个广播接收者
2.1导入jar包和so文件,在build.gradle里加入
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
public class JPushReceiver extends BroadcastReceiver {
String TAG="JPushReceiver"
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras()
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID)
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId)
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE))
//processCustomMessage(context, bundle)
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID)
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId)
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知")
String string = bundle.getString(JPushInterface.EXTRA_EXTRA)
Log.d(TAG, "string===="+string)
//打开自定义的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)
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA))
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false)
Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected)
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction())
}
}
}