Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-将用户发送到活动的GCM推送通知不会导致onCreate调用_Android_Push Notification_Google Cloud Messaging_Android Notifications - Fatal编程技术网

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

Android-将用户发送到活动的GCM推送通知不会导致onCreate调用,android,push-notification,google-cloud-messaging,android-notifications,Android,Push Notification,Google Cloud Messaging,Android Notifications,我能够创建推送通知,并将用户发送到我想要的任何活动,但我注意到,每当用户登录到该活动时,onCreate函数都不会被调用 应该是这样吗?如何设置它以调用活动的onCreate 以下是我的活动: public class QuestionActivity extends BaseListActivity { @Override public void onCreate(Bundle savedInstanceState) 以下是通知的生成方式: Intent noti

我能够创建推送通知,并将用户发送到我想要的任何活动,但我注意到,每当用户登录到该活动时,onCreate函数都不会被调用

应该是这样吗?如何设置它以调用活动的onCreate

以下是我的活动:

public class QuestionActivity extends BaseListActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
以下是通知的生成方式:

    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
,因为活动已经存在。

您是否
@覆盖了这些方法?发布您的代码。@323go覆盖哪个方法?我应该发布哪些代码?发布您用于启动活动的代码。我刚刚发布了活动开始和创建推送通知的代码。@Genadinik,这取决于您试图实现的目标。如果你的应用程序已经运行,并且相关活动已经存在,你真的需要创建该活动的新实例,还是可以使用现有的实例?好的,我需要它来重新加载该屏幕,以便它可以从服务器中提取内容。@Genadinik好的,也许您可以在
onResume
中从服务器重新加载数据,而不是
onCreate
。是否使用这些标志取决于您希望从通知中打开的活动如何与应用程序的其余部分相关(例如,当您点击该活动的“后退”按钮时将打开哪个活动)。