代码之家  ›  专栏  ›  技术社区  ›  Genadinik

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

  •  2
  • Genadinik  · 技术社区  · 13 年前

    我可以创建推送通知,并将用户发送到我想要的任何“活动”,但我注意到,每当用户登录该“活动”时,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);
    

    谢谢

    1 回复  |  直到 13 年前
        1
  •  2
  •   Eran    13 年前

    您正在使用的标志 Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP 意味着,如果活动已经在当前任务中运行,那么将使用旧的现有活动,而不是启动该活动的新实例。那样的话 onCreate 不会被调用,因为该活动已经存在。