我在我的应用程序中实现谷歌云消息服务(GCM)。我使用gcmIntent服务来创建一个挂起的意图并打开一个不是启动活动的活动。当应用程序打开时,它工作正常。但是当应用程序关闭时,它打开启动活动,而不是期望的活动。我试了所有我能找到的解决方案,但没有工作。我已经挣扎了一个多星期了。如有任何帮助,我将不胜感激。
我的代码public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
Notification notification;
NotificationCompat.Builder builder;
public GcmIntentService() {
super("GcmIntentService");
}
public static final String TAG = "GCM Demo";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
String [] message={extras.getString("abc"),extras.getString("zyx"),extras.getString("123"),extras.getString("456")};
if(UserDetails.getPushNotificationStatus(this)){
sendNotification(message);
}
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String[] msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(getBaseContext(), ShowShoutComment.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent =
PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);
Uri notificationUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(remote_picture)
.setContentText(Html.fromHtml(msg[2]))
.setContentTitle("Shout")
.setSound(notificationUri)
.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(msg[2])));
mBuilder.setContentIntent(contentIntent);
notification = new Notification();
notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_LIGHTS;
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
得到如下stacktrace
05-20 09:47:44.926: I/ActivityManager(753): START u0 {cmp=com.shout.shout/.activities.ShowShoutComment bnds=[0,153][1080,441]} from pid -1
05-20 09:47:44.926: W/ActivityManager(753): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.shout.shout/.activities.ShowShoutComment bnds=[0,153][1080,441] }
05-20 09:47:44.976: I/ActivityManager(753): Start proc com.shout.shout for activity com.shout.shout/.activities.ShowShoutComment: pid=4742 uid=10195 gids={50195, 3003, 1028, 1015}
05-20 09:47:45.016: W/ActivityThread(4742): Application com.shout.shout can be debugged on port 8100...
05-20 09:47:45.146: I/ActivityManager(753): START u0 {flg=0x4000000 cmp=com.shout.shout/.activities.ShoutFeed} from pid 4742
05-20 09:47:45.626: I/ActivityManager(753): Displayed com.shout.shout/.activities.ShoutFeed: +476ms (total +668ms)
把这个放到你的上面(service class)
Intent intent2 = new Intent(context, yourDesiredActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent2, 0);
做了一些改动。
private void sendNotification(Context context,String[] msg) {
....
Intent notificationIntent = new Intent(context, ShowShoutComment.class);
....
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
...
}