我无法弄清楚我哪里出错了。我需要获取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 :(得分: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)
您可以查看
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 检查 - FireBaseInstanceId service does not get registered
您是否在AndroidManifiest.xml文件中注册了Firebase服务
~*