【问题标题】:Push notification not coming if App is closed Php Android如果应用程序关闭,推送通知不会到来 PHP Android
【发布时间】:2020-11-28 09:37:00
【问题描述】:

我正在使用 PHP(使用 fcm)在 android 中处理推送通知,推送通知工作正常,但是每当我的应用程序“关闭”时我都没有收到任何推送通知(只有当我的应用程序“关闭”时我才会收到通知)打开”) 这是我的代码,我哪里错了?

function testingPush(){
    define("FCM_URL", "https://fcm.googleapis.com/fcm/send"); 
    define( "FCM_KEY","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

$devicetype1 = "ios";
                $devicetype2 = "android";
                $deviceType = $rows['device_type']; // coming from database
                $deviceToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$titleMessage="Push Notification Testing";
    $message="Lorem Ipsum";
            $msgs = array();
                        if (strcasecmp($devicetype1, $deviceType) == 0) 
                            {
                                /* sending push to IOS */
                                $notification = array('body'=>$message,     
                                'sound'=>"default",
                                'title'=>$titleMessage,
                                'badge'=>1);
                                $data = array('notificationType'=>1,        
                                'displayURL'=>"https://upload.wikimedia.org/wikipedia/commons/thumb/2/2a/FloorGoban.JPG/1024px-FloorGoban.JPG");
                                $msgs = array('to'=>$deviceToken,       
                                'content_available'=>true,
                                'mutable_content'=>true,
                                'notification'=>$notification,
                                'priority'=>'high',
                                'data'=>$data); 
                            }
                        else{
                                /* sening push to android */
                                $notification = array('contentTitle'=>$titleMessage,
                                'message'=>$message);
                                $msgs = array('to'=>$deviceToken,
                                'priority'=>'high',
                                'data'=>$notification);
                            }
                            $this->sendPushNotification_With($msgs);
}

 private function sendPushNotification_With($json) {
            $headers = array('Authorization: key=' . FCM_KEY,'Content-Type: application/json');
            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, FCM_URL );
            curl_setopt( $ch,CURLOPT_POST, true );
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode($json) );
            $result = curl_exec($ch );
            curl_close( $ch );
        }

【问题讨论】:

标签: php android firebase push-notification


【解决方案1】:

好吧,我认为问题是 $notification 中的 contentTitle ,将其替换为 title 并且它应该可以工作,并且在您的 ios 通知中您使用 notification 作为单独的变量,我宁愿建议您通过通知数据中的参数,因为通知在后台不起作用。如果以上不起作用,请重新检查您的密钥

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多