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

3

我希望在接收到来自服务器的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);    
    }

如果这个问题已经在别处提出,请向我重定向,我会删除我的问题。
提前致谢。
2个回答

4
如果我理解正确,你想直接启动应用程序而不是创建通知。
要实现这一点,你只需要使用以下代码替换generateNotification()的调用即可:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("your.package.name");
startActivity(LaunchIntent);

这是您需要的吗?


4
如果您不想收到通知,则您的代码不应该创建通知。 而是调用一个启动您的应用程序的主要Activity(或任何您想要您的应用程序在其中启动的Activity)的方法,而不是调用generateNotification
    Intent intent = new Intent().setClass(context, MainActivity.class);
    startActivity(intent);  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接