来自phpmyadmin的Android Push通知未收到通知

时间:2018-11-30 21:02:48

标签: php android firebase push-notification

首先,我进行了搜索,但2017/2018年没有结果,现在有些东西已弃用。

我正尝试从php发送程序通知。我有这个php(send_notification),但是从2016年开始。我已经将令牌存储到数据库并存储在tokens.tokens中。我认为从php发送的数据还不够,因为当我现在更改notifybuilder时,它说您需要指定idchannel(不推荐使用最后一个)

 manager.notify(1,notificationBuilder.build());

当我传递信息时($ fields)

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);

        return $result;

它不说频道。


更新:仅当网络休眠时才从网络发送该消息。我尝试了该测试,并发送给主题测试和通道1,并且两者都得到了,所以我很确定错误必须出在php上,也可能出在我在开始时标记的curl上。 TEST

TEST2

我现在将放置代码,以便有人帮助。

FirebaseMessagingService

package com.example.android.fcmtest;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.firebase.messaging.RemoteMessage;

import androidx.core.app.NotificationCompat;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {

    WebService ws;


    @Override
    public void onNewToken(String token) {

        super.onNewToken(token);
        Log.d("NEW_TOKEN",token);
        Context context = this;
        ws.registrarTokenWebService(token, context);
    }



    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        showNotification(remoteMessage.getData().get("message"));
    }

    private void showNotification(String message){

        Intent i =  new Intent(this,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
                .setAutoCancel(true)
                .setContentTitle("FCM Test")
                .setContentText(message)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                .setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(1,notificationBuilder.build());
    }

}

主要活动

package com.example.android.fcmtest;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;

public class MainActivity extends AppCompatActivity {
    WebService ws;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ws = new WebService();

        FirebaseMessaging.getInstance().subscribeToTopic("test");
        FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( new OnSuccessListener<InstanceIdResult>() {
            @Override
            public void onSuccess(InstanceIdResult instanceIdResult) {
                String deviceToken = instanceIdResult.getToken();
                ws.registrarTokenWebService(deviceToken, MainActivity.this);
            }
        });

    }
}

push_notification.php

<?php

    function send_notification ($tokens, $message)
    {
        $url = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
            'registration_ids' => $tokens,
            'data' => $message
            );

        $headers = array(
            'Authorization:key = -auth key-',
            'Content-Type: application/json'
            );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);

        return $result;

    }


    $hostname_localhost ="localhost";
    $username_localhost ="root";
    $password_localhost ="";
    $database_localhost ="tn";


    $conn = mysqli_connect ($hostname_localhost, $username_localhost, $password_localhost, $database_localhost);



    $sql = "SELECT tokens.tokens FROM tokens";


    $result = mysqli_query($conn,$sql);
    $tokens = array();

    if(mysqli_num_rows($result) > 0){

        while ($row = mysqli_fetch_assoc($result)) {
            $tokens[] = $row["tokens"];
        }
    }

    mysqli_close($conn);

    $message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
    $message_status = send_notification($tokens, $message);
    echo $message_status;
?>

从现在开始,我很高兴。

0 个答案:

没有答案