【发布时间】:2014-05-20 17:03:31
【问题描述】:
我正在我的应用中实施谷歌云消息服务 (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);
}
}
我得到以下堆栈跟踪
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)
【问题讨论】:
-
在您想要启动的活动中实现 sendNotification() 方法......
-
@NaveedAli 我无法理解。如何调用该方法。我无法理解您的回复:(
-
@ThirukumaranNagarajan:我不是安卓专家,但请尝试
PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, FLAG_UPDATE_CURRENT);并声明android:launchMode="singleTop"
标签: android google-cloud-messaging android-notifications