我有GCM通知实施。我知道客户端应用程序会收到通知,无论它是处于前台,后台还是被杀死状态。我想知道的是,当应用程序处于被杀死状态时,如何在收到通知时启动我的应用程序?
答案 0 :(得分:5)
在信息接收器中,我执行以下操作:
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
这里用条目活动替换YourActivity.class。这对我有用。
答案 1 :(得分:2)
您可以使用NotificationManager开始您的活动。
尝试在 onMessage()方法中使用以下代码。这是在扩展 GCMBaseIntentService 类GCM的类中重写的方法。
int icon = R.drawable.your_app_icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, YOUR_ACTIVITY.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// notificationIntent.putExtra("PostName", vPostText);
// Log.i(TAG, "Sent Postid " + postid);
// Util util = (Util) context.getApplicationContext();
// util.setPostID(postid);
// util.setNotify(true);
// util.setUserNAME(vPortCode);
// util.setPostNAME(vPostText);
// util.setmEDIA(vMedia);
// util.setmEDIATHHUMB(vMediaThumb);
// util.setmEDIATYPE(vMediaType);
// util.setAirportName(vAirportName);
notificationIntent.putExtra("Set_image", true);
notificationIntent.putExtra("Notify", true);
// set intent so it does not start a new activity
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
// | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context,
(int) System.nanoTime(), notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify((int) System.nanoTime(), notification);