Php 无法在MySQL数据库中存储Firebase令牌

Php 无法在MySQL数据库中存储Firebase令牌,php,android,mysql,firebase,firebase-cloud-messaging,Php,Android,Mysql,Firebase,Firebase Cloud Messaging,我想不出哪里出了问题。我需要获取firebase令牌并将其存储在我的数据库中,通过使用这些令牌,我需要向这些设备发送通知。下面是我的代码。我真的需要一些帮助 MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance

我想不出哪里出了问题。我需要获取firebase令牌并将其存储在我的数据库中,通过使用这些令牌,我需要向这些设备发送通知。下面是我的代码。我真的需要一些帮助

MainActivity.java

public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseInstanceId.getInstance().getToken();
    FirebaseMessaging.getInstance();


}

}
FireBaseInstancedService.java

public class FirebaseInstanceIDService extends 
FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {

    String token = FirebaseInstanceId.getInstance().getToken();
    Log.e("Token :",token);
    registerToken(token);
}

private void registerToken(String token) {

    OkHttpClient client = new OkHttpClient();
    RequestBody body = new FormBody.Builder()
            .add("Token",token)
            .build();

    Request request = new Request.Builder()
            .url("http://localhost/notify.php")
            .post(body)
            .build();

    try {
        client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
public class FirebaseMessagingService extends 

com.google.firebase.messaging.FirebaseMessagingService{

@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 builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Test")
            .setContentText(message)
            .setSmallIcon(R.drawable.gas45)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());
}


}
FirebaseMessagingService.java

public class FirebaseInstanceIDService extends 
FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {

    String token = FirebaseInstanceId.getInstance().getToken();
    Log.e("Token :",token);
    registerToken(token);
}

private void registerToken(String token) {

    OkHttpClient client = new OkHttpClient();
    RequestBody body = new FormBody.Builder()
            .add("Token",token)
            .build();

    Request request = new Request.Builder()
            .url("http://localhost/notify.php")
            .post(body)
            .build();

    try {
        client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
public class FirebaseMessagingService extends 

com.google.firebase.messaging.FirebaseMessagingService{

@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 builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Test")
            .setContentText(message)
            .setSmallIcon(R.drawable.gas45)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());
}


}
notify.php

<?php 
if (isset($_POST["Token"])) {

       $fcm_Token=$_POST["Token"];
       $conn = mysqli_connect("localhost","root","","fcm") or 
 die("Error connecting");
       $q="INSERT INTO users (Token) VALUES ( '$fcm_Token')"
          ."ON DUPLICATE KEY UPDATE Token = '$fcm_Token';";

  mysqli_query($conn,$q) or die(mysqli_error($conn));
  mysqli_close($conn);
}
?>

请更改您的查询

   $q="INSERT INTO users (Token) VALUES ( '$fcm_Token')"
      ."ON DUPLICATE KEY UPDATE Token = '$fcm_Token';";
对此

$q="INSERT INTO users (Token) VALUES ( '".$fcm_Token."')"
          ."ON DUPLICATE KEY UPDATE Token = '".$fcm_Token."';";
我认为您在PHP字符串中添加变量语法时犯了错误。 您应该像这样附加
$fcm_标记
,因为您已经在使用
操作符附加字符串


请试试这个。

您可以检查

  • 在没有代理的情况下检查internet连接并打开internet连接
  • 用新的
    google service.json
    替换您的
    google service.json,您可以在FirebaseConsole中获得它
  • 请检查您的设备中是否有google play服务,是否正常工作,没有google play服务firebase无法工作
  • 检查-

    您是否在AndroidManifiest.xml文件中注册了Firebase服务

    <!-- Firebase Notifications -->
            <service
                android:name="com.indus.corelib.notification.FirebaseMessagingService"
                android:exported="false" >
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
            <service
                android:name="com.indus.corelib.notification.FirebaseIDService"
                android:exported="false" >
                <intent-filter>
                    <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
                </intent-filter>
            </service>
    
    
    一个花药和一个祝你好运

    onTokenRefresh()不受欢迎获取令牌的最佳方法是:

       FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
                @Override
                public void onSuccess(InstanceIdResult instanceIdResult) {
                    String newToken = instanceIdResult.getToken();
                    Log.e("newToken", newToken);
                    SharedPreferences.Editor editor = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE).edit();
                    if (token!=null){
                        storetoken(newToken);
                    }
    
                }
            });
    
    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this,new OnSuccessListener()){
    @凌驾
    成功时公共无效(InstancedResult InstancedResult){
    字符串newToken=instanceIdResult.getToken();
    Log.e(“newToken”,newToken);
    SharedReferences.Editor=GetSharedReferences(“令牌优先”,模式私有).edit();
    if(令牌!=null){
    storetoken(纽托克);
    }
    }
    });
    
    请检查我之前的答案[此]()您是否通过onTokenRefresh方法获得令牌?否。。我不知道你的代码有什么问题?请记住:您是我们中唯一可以在调试器中运行此代码并告诉我们发生了什么的人。对不起,我不明白您在说什么。您是否检查了您的查询是否正常工作。。。我错过了
    query
    @Pranav-MS,他在获取代币后没有在android端获取代币PHP代码被执行,所以首先我们需要检查android端而不是PHP端