android 推送消息

Android开发中,通常会使用BroadcastReceiver来接受Push推送消息。当APP收到推送通知时,我们需要在通知的点击事件中加入自己的逻辑。比如跳转到MainActivity。
比如下面的代码(注意红色部分):
[java]  view plain copy print ?
  1. public void onReceive(Context context, Intent intent) {  
  2.         Bundle bundle = intent.getExtras();  
  3.         if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {  
  4.             String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);  
  5.             Log.d(TAG, "[JPushReceiver] 接收Registration Id : " + regId);  
  6.             //send the Registration Id to your server...  
  7.                           
  8.         } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {  
  9.             Log.d(TAG, "[JPushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));  
  10.           
  11.         } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {  
  12.             Log.d(TAG, "[JPushReceiver] 接收到推送下来的通知");  
  13.             int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);  
  14.             Log.d(TAG, "[JPushReceiver] 接收到推送下来的通知的ID: " + notifactionId);  
  15.               
  16.         } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {  
  17.             Log.d(TAG, "[JPushReceiver] 用户点击打开了通知");  
  18.             JPushInterface.reportNotificationOpened(context, bundle.getString(JPushInterface.EXTRA_MSG_ID));  
  19.               
  20.             <span style="color:#ff0000;">// 打开自定义的Activity  
  21.             Intent i = new Intent(context, MainTabActivity.class);  
  22.             i.putExtras(bundle);  
  23.             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  24.             context.startActivity(i);</span>  
  25.               
  26.         } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {  
  27.             Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));  
  28.             //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..  
  29.               
  30.         } else {  
  31.             Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());  
  32.         }  
  33. }  

使用Activity以外的content来startActivity,必须指定为Intent.FLAG_ACTIVITY_NEW_TASK。
[java]  view plain copy print ?
  1. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  


我们可以尝试下使用Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT,会抛出以下异常:

[java]  view plain copy print ?
  1. 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



所以只能使用FLAG_ACTIVITY_NEW_TASK。使用这个还有个问题,就是会新打开一个MainActivity。如何解决这个问题?
其实很简单在AndroidManifest.xml中将MainActivity定义为:
 android:launchMode="singleTask" 即可:
[html]  view plain copy print ?
  1. <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 推送消息)