【问题标题】:Click on notification doesn't start a specific activity单击通知不会启动特定活动
【发布时间】:2014-04-04 08:49:01
【问题描述】:

我正在使用一个通知,其中我添加了一个应该启动一个活动的“动作”。

我有两个主要活动:登录活动和 MainActivity。当 LoginActivity 启动时,它会自动登录用户(如果用户以前登录过),做一些其他的事情,然后启动 MainActivity。 MainActivity 是多个 Fragment 的宿主。

现在当我像这样添加 LoginActivity 时:

 Intent notificationIntent;
 notificationIntent = new Intent(getActivity(), MainActivity.class);

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
         Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent intentp =
         PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);            

    NotificationCompat.Builder nBuilder;
    Uri alarmSound = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    nBuilder = new NotificationCompat.Builder(getActivity())
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Smart Share - " + "")
            .setLights(Color.BLUE, 500, 500).setContentText("message")
            .setAutoCancel(true).setTicker("Notification from smartshare")
            .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setContentIntent(intentp);
    NotificationManager nNotifyMgr = (NotificationManager) getActivity()
            .getSystemService(getActivity().NOTIFICATION_SERVICE);
   Notification not = nBuilder.build();
   nNotifyMgr.notify(2 + 2, not);

一切正常。当我将 MainActivity 放入 notificationIntent 时,单击通知什么也不做。

怎么会?

编辑:

我的清单文件中的片段:

  <activity
        android:name="<packagename>.LoginActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateVisible|adjustResize" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>  
    </activity>

    <activity
        android:name="<packagename>.MainActivity"
        android:label="@string/tab_title_mapview" 
        android:screenOrientation="portrait">
    </activity>

【问题讨论】:

  • 你能分享你的清单文件吗?
  • 你想念 mManager.notify(100, notification);看我的回答
  • 或者把所有代码发给你。
  • 你的mainActivity属于同一个包?试试这个nNotifyMgr.notify(1, not);

标签: android notifications action


【解决方案1】:

试试这个家伙其实你想念mManager.notify(100, notification);

NotificationManager mManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new Notification(R.drawable.ic_launcher, "Tittle", System.currentTimeMillis());
                RemoteViews contentView = new RemoteViews(getBaseContext().getPackageName(), R.layout.customnotification_alert);
                contentView.setProgressBar(R.id.progressBar, 10, 0, false);        
                contentView.setTextViewText(R.id.text, "text ");       
                notification.contentView = contentView;

                Intent notificationIntent = new Intent(getBaseContext(), NotifyMessage.class);
                PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);
                notification.contentIntent = contentIntent;
                mManager.notify(100, notification);

【讨论】:

  • 有什么理由要添加RemoteViews 吗?
  • 他想念 mManager.notify(100, notification);看我的回答
  • @Bhanu Sharma 我发布了用于创建通知的整个代码,我没有错过 .notify,我只是最初没有发布它。 :)
  • 尝试使用 notification.setLatestEventInfo
【解决方案2】:

试试这个:

PendingIntent contentIntent = PendingIntent.getActivity(getActivity(),0,PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_ONE_SHOT);

【讨论】:

  • afaik 这不是必需的,因为我使用的是 '.setContentIntent(intentp);'在构建器内部。
  • 如果不是这样,那么您的确切问题是什么。
  • 当我将 LoginActivity 放入 PendingIntent 时,这个 Activity 启动得很好。当我将 MainActivity 放入 PendingIntent 时,单击通知只会使通知消失(它应该消失),但不会启动所需的活动。
  • 非常感谢,成功了!您是否还知道如果活动在后台,我如何恢复活动?
【解决方案3】:

试试这个

    Notification notification = new Notification(icon, message, when);
 notificationIntent = new Intent(getActivity(), LoginActivity.class);
 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
     Intent.FLAG_ACTIVITY_SINGLE_TOP);
 PendingIntent intentp =
       PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);

notification.setLatestEventInfo(context, title, message, notificationIntent );

【讨论】:

    【解决方案4】:

    遇到了同样的问题,我通过以下方法解决了:

    将此添加到您的清单(在活动标签内)

    android:launchMode="singleTop"
    

    并将以下标志设置为您要开始的意图:

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    希望对你有帮助!

    【讨论】:

    • 不幸的是这没有帮助:(
    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多