简体   繁体   English

当设备收到GCM推送通知时,会弹出Android应用程序

[英]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. 我想在接收来自服务器的gcm通知或消息时实现一个应用程序弹出。 Generally a server sends message to GCM api and from there the message is sent to client. 通常,服务器向GCM api发送消息,并从那里将消息发送给客户端。

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: 为此,您只需要使用以下代码替换对generateNotification()的调用:

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). 而不是调用generateNotification调用一个方法来启动您的应用程序的主要Activity (或您希望您的应用程序开始的任何Activity )。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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