Android - 通过GCM发送推送通知,应用程序位于前台/后台

时间:2016-02-22 20:36:42

标签: android push-notification google-cloud-messaging gcmlistenerservice

我通过GCM将推送通知添加到我的应用中,但我遇到了困难。

当我在前台(打开和屏幕)上发送带有我的应用程序的推送通知时,它会按预期正常显示:

Push Notification with app on Foreground

但是当我在背景上用我的应用程序发送它时,它显示如下:

Push Notification with app on Background

因此我的大图像和图标背景颜色不会显示。

GCM HTTP呼叫代码:

{
  "notification": {
    "title": "Tecolutla, Veracruz",
    "body": "¡Bienvenidos a Tecolutla!",
    "icon": "push"
  },
  "priority": "high",
  "data": {
    "Mensaje": "Descubre muchísimos lugares en Visita Xalapa.",
    "Class" : "Festividades"
  },
  "to": "/topics/global"
}

GsmListenerService的代码:

@SuppressWarnings("ConstantConditions")
    private void sendNotification(Bundle data) {
        Intent intent = new Intent(this, TecolutlaVeracruz.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("Push_Title", data.getBundle("notification").getString("title"));
        intent.putExtra("Push_Body", data.getBundle("notification").getString("body"));
        intent.putExtra("Push_Mensaje", data.getString("Mensaje"));
        int Color = ColorManager.getColorBase(Main.INICIO);
        if(data.containsKey("Class")){
            if(data.getString("Class").equals("Inicio")) Color = ColorManager.getColorBase(Main.INICIO);
            else if(data.getString("Class").equals("Nuestro Municipio")) Color = ColorManager.getColorBase(Main.NUESTRO_MUNICIPIO);
            else if(data.getString("Class").equals("Festividades")) Color = ColorManager.getColorBase(Main.FESTIVIDADES);
        }
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.push)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.acercade_applogo))
                .setContentTitle(data.getBundle("notification").getString("title"))
                .setContentText(data.getBundle("notification").getString("body"))
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setColor(Color)
                .setVibrate(new long[]{ 0, 100, 200, 300 })
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

我遇到的另一个问题是,如果应用程序被终止或未运行,通知将无法显示。这是正常的吗?

1 个答案:

答案 0 :(得分:3)

因此我的大图像和图标背景颜色不会显示。

这是因为Android现在使用Material设计,推送的默认图标将完全为白色。此外,大图标应始终具有背景(即化身)。它以不同的背景颜色显示,因此它应该是不透明的图片。

请参阅此SO问题:Android Notification Large Icon not workAndroid Notification Large Icon not work

我遇到的另一个问题是,如果应用程序被终止或未运行,通知将不会显示。这是正常的吗?

Android通知是作为由BroadcastReceiver启动的服务实现的。当应用程序强制关闭时您不再收到通知的原因是因为此服务也已强行关闭。启动服务的BroadcastReceiver侦听ACTION_BOOT_COMPLETED事件和ACTION_USER_PRESENT事件。这意味着服务器在启动时启动,并在用户解锁锁定屏幕时重新启动。

如上所述here

  

只要您没有明确地杀死它,您的应用就可以接受GCM广播。一旦你杀了它,它将不会收到任何广播,直到你下次启动它。但是,如果您将应用程序移至后台(例如,通过启动另一个应用程序或点击后退按钮,直到您移至主屏幕或其他应用程序),您的应用程序仍会从GCM收到消息。

因此,如果您通过转到设置强制关闭应用,则推送通知将停止工作。当您再次打开应用程序时,您将收到通知。