又一个通知没有开始活动

时间:2015-04-24 16:22:03

标签: android notifications

我已经在StackOverflow上阅读了(差不多)与此问题相关的所有其他问题。

问题通常是:当我点按我的应用发布的Notification时,相关的Activity未启动。这是代码:

NotificationManager notificationManager = 
             (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

... String json is prepared ...
Intent intentForActivity = new Intent(this, MyActivity.class);

Bundle extras = new Bundle();
extras.putString(Activity.KEY_JSON, json);
intentForActivity.setFlags(
      Intent.FLAG_ACTIVITY_NEW_TASK | 
      Intent.FLAG_ACTIVITY_CLEAR_TOP |
      Intent.FLAG_ACTIVITY_CLEAR_TASK);
intentForActivity.putExtras(extras);

PendingIntent pendingIntent = 
    PendingIntent.getActivity(this, 
                              0, 
                              intentForActivity,
                              PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

... various builder methods for icon, title, message ...

builder.setContentIntent(pendingIntent);
notificationManager.notify(NOTIFICATION_ID++, builder.build());

一些注意事项:

  • 我尝试了各种标志和排列,没有任何改变;
  • 我必须PendingIntent.FLAG_UPDATE_CURRENTString json更新extras,否则Android会继续使用第一个。{/ li>

1 个答案:

答案 0 :(得分:0)

这是我一直在使用的,它对我有用。代码是自我解释的。

private void showNotification(final String title, String text, int ID, boolean showTimeStamp) {

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) //Use a builder
            .setContentTitle(title) // Title
            .setContentText(text) // Message to display
            .setTicker(text).setSmallIcon(R.drawable.ic_notif_small) // This one is also displayed in ticker message
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bulb)); // In notification bar

    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    //mBuilder.addAction(R.drawable.bulb_small, "OK", resultPendingIntent);

    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS | Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

    long time = 0;
    if (showTimeStamp)
        Calendar.getInstance().getTimeInMillis();
    else
        time = android.os.Build.VERSION.SDK_INT >= 9 ? -Long.MAX_VALUE : Long.MAX_VALUE;

    notification.when = time;

    mNotificationManager.cancel(ID);
    mNotificationManager.notify(ID, notification);
}

并将此android:launchMode="singleTask"添加到您的主要活动的清单

示例:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>