Android的 - GCM推送通知发送用户到活动不会导致通话的onCreate通知、用户、Android、GCM

2023-09-06 10:51:55 作者:八月、你可知。

我能够创建推送通知,并送我想任何活动的用户,但我注意到,每当用户的土地上活动,该功能的onCreate不会被调用。

I am able to create push notifications, and to send the user to whichever Activity I want, but I notice that whenever the user lands on that Activity, the onCreate function does not get called.

是,应该是这样吗?如何设置它,使活动的OnCreate叫?

Is that supposed to be the case? How do I set it so that the onCreate of the Activity is called?

下面是我的活动:

public class QuestionActivity extends BaseListActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 

和这里是如何生成的通知:

and here is how the notification is generated:

    Intent notificationIntent = new Intent(context, MainActivity.class);
    if ( notification_type != null && notification_type.equals("question"))
    {
        notificationIntent = new Intent(context, QuestionActivity.class);
    }
    else
    if ( notification_type != null && notification_type.equals("plan"))
    {
        notificationIntent = new Intent(context, TopicActivity.class);              
    }

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);        
    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);            

    Notification notification = new NotificationCompat.Builder(context)
     .setContentTitle(title)
     .setContentText(message)
     .setContentIntent(intent)
     .setSmallIcon(icon)
     .setLights(Color.YELLOW, 1, 2)
     .setAutoCancel(true)
     .setSound(defaultSound)
     .build();

    notificationManager.notify(0, notification);

谢谢!

推荐答案

这是您所使用的标志 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP 意味着如果活动已在当前任务运行,则代替启动该活动的一个新实例,旧的现有活动将被使用。在这种情况下,的onCreate 将不会被调用,因为该活动已经存在。

The flags that you are using Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP mean that if the activity is already running in the current task, then instead of launching a new instance of that activity, the old existing activity will be used. In that case onCreate won't be called, since the activity already exists.