Android - 在 foreground/background 中使用应用程序通过 GCM 发送推送通知

Android - Sending Push Notifications via GCM with app in foreground/background

我正在通过 GCM 将推送通知添加到我的应用程序,但我遇到了问题。

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

但是当我在后台使用我的应用程序发送它时,它显示如下:

所以我的大图和图标背景色没有显示。

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

我遇到的另一个问题是,无论应用是否被终止,通知都不会显示 运行。这正常吗?

所以我的大图和图标背景颜色不显示。

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

请参考这个问题: and

我遇到的另一个问题是,无论应用是否被终止,通知都不会显示 运行。这正常吗?

Android 通知实现为由 BroadcastReceiver. The reason you are no longer receiving notification when the app is force-close is because this Service has also been force-close. The BroadcastReceiver which starts the Service listens to the ACTION_BOOT_COMPLETED event and the ACTION_USER_PRESENT 事件启动的服务。这意味着服务器在启动时启动,并在用户解锁锁定屏幕时重新启动。

如前所述here

Your app can accept GCM broadcasts only as long as you don't kill it explicitly. Once you kill it, it won't receive any broadcasts until the next time you launch it. If, however, you move your app to the background (for example by launching another app or hitting the back button until you move to the home screen or to another app), your app would still get messages from GCM.

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