简体   繁体   中英

Android app pop up when a GCM push notification is received on the device

I want to implement an app pop up on receiving the gcm notification or message from server. Generally a server sends message to GCM api and from there the message is sent to client.

On the client side generally you get a notification but i don't want that i want the client device to activate or open or start that particular app.

Is it possible? if so how?

the following is my notification generate function

  private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.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);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);    
    }

and if this question is already asked somewhere kindly redirect me and ill delete my question.

Thanks in advance.

If I understood correctly, what you want is to start the app directly instead of create the notification.

In order to do that you just need to replace the call to generateNotification() with this code:

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("your.package.name");
startActivity(LaunchIntent);

Is that what you need?

If you don't want to get a notification, then your code shouldn't create a notification. Instead of calling generateNotification call a method that starts the main Activity of your app (or whatever Activity you want your app to start in).

    Intent intent = new Intent().setClass(context, MainActivity.class);
    startActivity(intent);  

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