Android 应用程序关闭时打开启动活动的通知

Android 应用程序关闭时打开启动活动的通知,android,google-cloud-messaging,android-notifications,Android,Google Cloud Messaging,Android Notifications,我正在我的应用程序中实现谷歌云消息服务(GCM)。我使用gcminent服务创建了一个挂起的意图,并打开了一个不是启动活动的活动。当应用程序打开时,它工作正常。但当应用程序关闭时,它会打开启动活动,而不是所需的活动。我尝试了所有我能找到的解决办法,但没有任何效果。我挣扎了一个多星期。任何帮助都将不胜感激 我的代码 public class GcmIntentService extends IntentService { public static final int NOTIFICATI

我正在我的应用程序中实现谷歌云消息服务(GCM)。我使用gcminent服务创建了一个挂起的意图,并打开了一个不是启动活动的活动。当应用程序打开时,它工作正常。但当应用程序关闭时,它会打开启动活动,而不是所需的活动。我尝试了所有我能找到的解决办法,但没有任何效果。我挣扎了一个多星期。任何帮助都将不胜感激

我的代码

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)
把这个放在你的上面(服务类)

希望这将对您有所帮助。

做了一些更改

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);
   ...    
}

在您想要启动的活动中实现sendNotification()方法……@NaveedAli我无法理解。如何调用该方法。我无法理解您的回答:(@thirukumaranagarajan:我不是安卓专家,但请尝试
pendingent.getActivity(getBaseContext(),0,notificationIntent,FLAG_UPDATE_CURRENT);
并声明
android:launchMode=“singleTop”
我将从哪里获得上下文?我使用的是getBaseContext()。是否有方法获取服务中的上下文?是否尝试使用
this
而不是
getBaseContext()
context
?请尝试使用getBaseContext();
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);
   ...    
}