且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android - 通过前景/背景中的应用通过GCM发送推送通知

更新时间:2023-01-02 23:10:32

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



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



请参阅这个SO问题: Android通知大图标不起作用



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

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

正如


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


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


I'm adding Push Notifications via GCM to my app, but I am stuck on something.

When I send a Push Notification with my app on foreground (open and on screen), it displays normally, as intended:

But when I send it with my app on background, it displays like this:

So my large image and icon background color doesn't display.

Code for GCM HTTP Call:

{
  "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"
}

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

Another issue I have, is that the notification won't display if the app is killed or not running. Is this normal?

So my large image and icon background color doesn't display.

This is because Android now uses Material design and the default icon for push will be completely white. Also, a large icon should always have a background (i.e. avatar). It is displayed on different background colors, therefore it should be a non-transparent picture.

Please refer to this SO questions: Android Notification Large Icon not work and Android Notification Large Icon not work

Another issue I have, is that the notification won't display if the app is killed or not running. Is this normal?

Android notification is implemented as a Service that is started by a 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 event. This means the server starts at boot and restarts whenever the user unlocks the lock screen.

As stated 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.

So if you force-close your app by going to settings, then push notifications will stop working. When you open the app again, then you will receive the notification.