Pergunta

I have tested the intent of notification about 4 or 5 days. I diappointed myself and I can't reach my goal. I think it's not too difficult but I can't get it. I want to go current running activity when I click the notification. I used many things in intent.

   notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
   notificationIntent.setAction(Intent.ACTION_MAIN);
   notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

In pendingIntent,

     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

In manifest,

    android:launchMode="singleTop" // android:launchMode="singleTask" // android:launchMode="singleInstance"

but it always go the class I give at intent.Somebody help me, I have no idea.

Foi útil?

Solução

Try something like:

@SuppressLint("NewApi")
public static void showNotification(Context context, String message) {
    Intent intent = new Intent(context, MainActivity.class);
    // Ensures navigating back to activity, then to Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Add the activity parent chain to the stack builder.
    stackBuilder.addParentStack(MainActivity.class);
    // Add a new Intent to the task stack.
    stackBuilder.addNextIntent(intent);     
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Create notification.
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        // API 16 onwards.
        Notification.Builder builder = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setStyle(new Notification.BigTextStyle().bigText(message)) 
            .setWhen(System.currentTimeMillis());
            mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    } else {
        // API 15 and earlier.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis());
        mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top