2017-09-14 83 views
2

我無法弄清楚我要出錯的地方。我需要獲取firebase令牌並將其存儲在我的數據庫中,並使用這些令牌將通知發送到這些設備。以下是我的代碼。我真的需要一些幫助。無法在MySQL數據庫中存儲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); 
} 
?> 
+0

請查看我以前的答案[這](https://stackoverflow.com/a/40283178/4741746) –

+0

你得到onTokenRefresh的方法令牌? –

+0

不..我沒有得到 –

回答

1

請查詢

$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,因爲您已經使用.運算符附加了字符串。

請試試這個。

+0

它仍然沒有工作.. –

+0

你檢查你的工作是否正常? ??? –

+0

對不起,我不明白你在說什麼。 –

0

您可以檢查

  1. 請與無代理和開放的互聯網您的互聯網連接進行連接
  2. 更換新的google-service.json您可以在firebaseconsol得到這個
  3. 請檢查您的設備有谷歌玩它的服務和它的工作與否,firebase不工作沒有谷歌播放服務

檢查 - FireBaseInstanceId service does not get registered

你已經註冊在AndroidManifiest.xml您的火力地堡服務文件

<!-- 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> 

請點擊此step 一個花葯examplebest一個好運