标签:android开发 broadcastreceiver
Android开发中,通常会使用BroadcastReceiver来接受Push推送消息。当APP收到推送通知时,我们需要在通知的点击事件中加入自己的逻辑。比如跳转到MainActivity。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, "[JPushReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[JPushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[JPushReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[JPushReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[JPushReceiver] 用户点击打开了通知");
JPushInterface.reportNotificationOpened(context, bundle.getString(JPushInterface.EXTRA_MSG_ID));
// 打开自定义的Activity
Intent i = new Intent(context, MainTabActivity.class);
i.putExtras(bundle);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
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 {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
更多关于Intent的flag,可以参考:http://developer.android.com/reference/android/content/Intent.html
<activity android:name="com.withiter.quhao.activity.MainTabActivity" android:launchMode="singleTask" android:label="@string/app_name" android:screenOrientation="portrait">
这样每次打开推送,就不会出现2个activity的情况了。
更多关于activity的launchMode可以参考:http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
好了今天这个破事就到这里,其实android开发就这么点破事。关于其他破事,见专栏:
更多Android开发的破事,请看专栏:《Android开发那点破事》
【Android开发那点破事】消息推送BroadcastReceiver,点击通知打开两次Activity问题,布布扣,bubuko.com
【Android开发那点破事】消息推送BroadcastReceiver,点击通知打开两次Activity问题
标签:android开发 broadcastreceiver
原文地址:http://blog.csdn.net/withiter/article/details/30476283