我正在我的应用中实施谷歌云消息服务(GCM)。我使用gcmIntent服务创建待处理的意图并打开一个不是启动活动的活动。当应用程序打开时,它工作正常。但是当app关闭时,它会打开启动活动而不是所需的活动。我尝试了所有可以找到的解决方案,但没有任何工作。我苦苦挣扎了一个多星期。任何帮助将不胜感激。
我的代码
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)
答案 0 :(得分:1)
把它放在上面的(服务类)
Intent intent2 = new Intent(context, yourDesiredActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent2, 0);
希望这会对你有所帮助。
答案 1 :(得分:1)
做了一些改变。
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);
...
}