繁体   English   中英

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

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

我想在接收来自服务器的gcm通知或消息时实现一个应用程序弹出。 通常,服务器向GCM api发送消息,并从那里将消息发送给客户端。

在客户端通常您会收到通知,但我不希望我希望客户端设备激活或打开或启动该特定应用程序。

可能吗? 如果是这样的话?

以下是我的通知生成功能

  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);    
    }

如果这个问题已经被问到某个地方,那么请重新定向我并删除我的问题。

提前致谢。

如果我理解正确,你想要的是直接启动应用程序而不是创建通知。

为此,您只需要使用以下代码替换对generateNotification()的调用:

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

这就是你需要的吗?

如果您不想收到通知,那么您的代码不应该创建通知。 而不是调用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