【问题标题】:Android - Sending Push Notifications via GCM with app in foreground/backgroundAndroid - 通过 GCM 在前台/后台使用应用程序发送推送通知
【发布时间】:2016-02-22 20:36:41
【问题描述】:

我正在通过 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 push-notification google-cloud-messaging gcmlistenerservice


    【解决方案1】:

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

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

    请参考这个 SO 问题:Android Notification Large Icon not workAndroid Notification Large Icon not work

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

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

    here所述:

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

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

    【讨论】:

    • 所以如果应用程序在后台,颜色和大图标不会显示?有什么解决办法吗?
    猜你喜欢
    • 2016-01-16
    • 1970-01-01
    • 2015-10-10
    • 2016-07-12
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多