【问题标题】:How to send push notification to android devices even when app is killed?即使应用程序被杀死,如何向安卓设备发送推送通知?
【发布时间】:2019-04-24 07:20:10
【问题描述】:

当应用程序处于前台和后台时,我可以发送通知。但是当应用程序被杀死时无法发送它,即应用程序没有在后台运行。我手机中的其他应用程序即使在后台运行时也能够向我发送通知。我正在使用奥利奥版本。

我也用“数据”替换了“通知”,这并没有什么不同。 我已经在 onMessageReceived 方法上添加了自定义通知,“通知”和“数据”都在前台和后台提供通知。唯一的区别是“数据”也在后台运行 onMessageReceived 方法。但是在两者上,当应用程序被杀死时都没有收到通知。我在 php 上尝试了以下代码。我做错了什么?

function sendPushNotification($token) {

    $url = "https://fcm.googleapis.com/fcm/send";

    $serverKey = 'AAAA.....theKey';
    $title = "My App";
    $body = "hello there!!";
    $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
 /*   if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }*/
    curl_close($ch);

   // echo "<br>";

   return $response;

}

在 onMessageReceived 方法之后:

对于“通知”:

public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d("apkflow","onMessageReceived Started");

    if (remoteMessage.getNotification() != null) {
        title = remoteMessage.getNotification().getTitle();
        body = remoteMessage.getNotification().getBody();

        Log.d("apkflow","title = " + title);
        Log.d("apkflow","body = " + body);
    }
}

对于“数据”:

        title = remoteMessage.getData().get("title");
        body = remoteMessage.getData().get("body");

更新:: 我现在得到了解决方案!它是由于移动最近更新。在vivo、oppo、xiomi等手机中,当app被清除时,会强制停止应用程序强制停止所有服务。因此,FCM 服务也停止了,移动设备上也没有收到任何通知。因此,为了获得通知,用户必须允许应用程序在后台运行,必须选中“允许在后台”。这解决了问题。如果还有问题,请留言!!

【问题讨论】:

    标签: android firebase push-notification firebase-cloud-messaging


    【解决方案1】:

    同时包含通知和数据负载的消息,当在 背景。

    更改notification 类型data

    $arrayToSend = array('to' => $token, 'data' => $notification,'priority'=>'high');
    

    请仔细阅读以下文档 https://firebase.google.com/docs/cloud-messaging/android/receive

    您必须创建自定义通知。

    private void setNotification(RemoteMessage content) {
            Log.d(TAG, "custom notification: ");
            Intent intent = new Intent(this, NotificationActivity.class);
            if (!content.getData().get("url").isEmpty())
                intent.putExtra("url", content.getData().get("url"));
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setAction(Long.toString(System.currentTimeMillis()));
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
    
    
            RemoteViews remoteViews = new RemoteViews(getPackageName(),
                    R.layout.custome_notification);
    
            remoteViews.setTextViewText(R.id.tvTime, currentDate());
            remoteViews.setTextViewText(R.id.text, content.getData().get("text"));
    
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getPackageName())
                    .setSmallIcon(R.drawable.ic_alert)
                    .setContent(remoteViews)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri);
            notificationBuilder.setContentIntent(pendingIntent);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            // To avoid replacing old notification by new one. To set new id for every new Notification following notifications.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = new NotificationChannel(getPackageName(), "AppName", importance);
                notificationManager.createNotificationChannel(mChannel);
            }
            int notifyId = (int) System.currentTimeMillis();
            notificationManager.notify(notifyId, notificationBuilder.build());
        }
    

    【讨论】:

    • 我已经添加了自定义通知,“通知”和“数据”都在前台和后台提供通知。唯一的区别是“数据”也在后台运行 onMessageReceived 方法。但是在两者上,当应用程序被杀死时都不会收到通知。
    • 删除 notification 仅添加 data
    • 我也做了同样的兄弟!我现在得到了解决方案!它是由于移动最近更新。在vivo、oppo、xiomi等手机中,当app被清除时,会强制停止应用程序强制停止所有服务。因此,FCM 服务也停止了,移动设备上也没有收到任何通知。因此,为了获得通知,用户必须允许应用程序在后台运行,必须选中“允许在后台”。这样就解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2017-06-07
    • 2015-12-30
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多