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

时间:2017-09-14 10:34:12

标签: php android mysql firebase firebase-cloud-messaging

我无法弄清楚我哪里出错了。我需要获取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();


}

}

FirebaseInstanceIDService.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();
    }
}
}

FirebaseMessagingService.java

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);
}
?>

3 个答案:

答案 0 :(得分:1)

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);
                }

            }
        });

答案 1 :(得分:0)

请更改您的查询

   $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_Token,因为您已经使用.运算符附加了字符串。

请试一试。

答案 2 :(得分:0)

您可以查看

  1. 检查您的互联网连接,无需代理并打开互联网连接
  2. 将您的google-service.json替换为新的<!-- 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> ,您可以在firebaseconsol
  3. 中获取此内容
  4. 请检查您的设备是否有谷歌播放服务,它是否正常工作,如果没有谷歌播放服务,firebase无法正常工作
  5. 检查 - FireBaseInstanceId service does not get registered

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

    ~*

    请关注此step 一个花药examplebest一个祝你好运