如果应用已关闭,则Android不会推送通知

时间:2020-08-08 05:23:49

标签: php android firebase push-notification

我正在使用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 );
        }

1 个答案:

答案 0 :(得分:0)

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