简体   繁体   中英

Android notification Resume Activity

I'm trying to make a notification when users pause my app. So to make it easier, users can go quickly to the application using notificaction. This is the code i'm using. It works for all versions before android 4 and i don't know which is the problem

 NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)    
                .setContentTitle("Titulo")
                .setContentText("Titulo");

        mBuilder.setOngoing(true);

        // Creates an explicit intent for an Activity this 
        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        // put the flags
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // mId allows you to update the notification later on.
        mNotificationManager.notify(mId, mBuilder.build());

So when i press the notification in android 4.0 and higher, the activity is created again instead of resume. any help please, i can't make it work.

EDIT ( forget about manifest singletop )

android:launchMode="singleTop" same result, not working...

My activity contains a map. I'm using the new version of google maps. v2.

I just tried PendingIntent.FLAG_CANCEL_CURRENT and seems to work for me

public void showNotification(String header,String message){

            // define sound URI, the sound to be played when there's a notification
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Intent intent = new Intent(this, MainActivity.class);

            //PendingIntent.FLAG_CANCEL_CURRENT will bring the app back up again
            PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,PendingIntent.FLAG_CANCEL_CURRENT, intent, 0);

            Notification mNotification = new Notification.Builder(this)
                .setContentTitle(header)
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pIntent)
                .setSound(soundUri)

                .addAction(R.drawable.ic_launcher, "View", pIntent)
                .addAction(0, "Remind", pIntent)
                .setOngoing(true)//optional
                .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(0, mNotification);
}

The only solution that actually worked for me after doing a lot of search is to do the following :

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

Intent resultIntent = new Intent(this, MainClass.class);
resultIntent.setAction("android.intent.action.MAIN");
resultIntent.addCategory("android.intent.category.LAUNCHER");

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());

this opens your current activiy without creating another one !

MainActivity的清单声明中使用android:launchMode="singleTop"

SOLUTION provided by @Patrick taken from the question and added as answer:

NotificationCompat.Builder mBuilder = 
                new NotificationCompat.Builder(this) 
                .setSmallIcon(R.drawable.logo)    
                .setContentTitle(getString(R.string.txt)) 
                .setContentText(getString(R.string.txt)); 

        mBuilder.setOngoing(true); 

        // Creates an explicit intent for an Activity in your app 
        Intent resultIntent = new Intent(this, Activity.class); 

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

        // The stack builder object will contain an artificial back stack for the 
        // started Activity. 
        // This ensures that navigating backward from the Activity leads out of 
        // your application to the Home screen. 
        TaskStackBuilder stackBuilder = TaskStackBuilder.from(this); 
        // Adds the back stack for the Intent (but not the Intent itself) 
        stackBuilder.addParentStack(Activity.class); 
        // Adds the Intent that starts the Activity to the top of the stack 
        stackBuilder.addNextIntent(resultIntent); 
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0); 
        mBuilder.setContentIntent(resultPendingIntent); 
        NotificationManager mNotificationManager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
        // mId allows you to update the notification later on. 
        mNotificationManager.notify(mId, mBuilder.getNotification());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM