这是我第一次使用FCM,我在android studio中创建了一个新项目,然后点击了“工具”,我选择了Firebase,I followed all the steps in this window
and I copied and pasted these blocks of code exactly in the same location as mentioned
这是我的Java代码
public class ZeftToken extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d("zeft", "Refreshed token: " + refreshedToken);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这是我的清单文件:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ZeftToken">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
当我运行应用程序时,我没有在logcat中获取令牌,而且它完全丢失了。我不知道出了什么问题。
答案 0 :(得分:1)
我用下面的这个函数解决了我的解决方案;
public void savePushToken(){
if (!settings.hasData(Keys.pushTokenSaved) ){
FirebaseMessaging messaging = FirebaseMessaging.getInstance();
messaging.getToken().addOnSuccessListener(s -> {
Log.d("ON TOKEN",s);
pushToken = s;
JSONObject params = new JSONObject();
JSONObject info = new JSONObject();
try {
info.put("os","android");
info.put( "framework", "flutter");
info.put( "cihaz_bilgisi", new JSONObject());
params.put("token",pushToken);
params.put("device_info",info);
} catch (JSONException e) {
e.printStackTrace();
}
NetworkHelper.request(MyMethod.put, Links.savePushToken, params, false, false,
response->{
Log.d("PUSH_REQUEST",response.toString());
if (response.has("successful")||response.optString("message").contains("Integrity constraint violation: 1062 Duplicate entry")) {
settings.setBool(Keys.pushTokenSaved, true);
}
});
});
}
}
答案 1 :(得分:0)
也许您已经在之前的应用实例中收到了令牌。方法onTokenRefresh
仅在创建或更新时被调用。它不会在所有应用程序实例中执行。清除应用数据或卸载应用并再次运行,以便创建新令牌,您可以在日志中看到它。
确保将收到的令牌存储在共享首选项或数据库中以备将来使用。
答案 2 :(得分:0)
如果其他好的话。 (我希望你有fcm服务提供的json文件?) 只需移动
FirebaseInstanceId.getInstance().getToken();
例如您的应用程序或主要活动
如果你有错误(空指针异常)而不是firebase未初始化。检查againe教程。并且不要忘记google-json
答案 3 :(得分:0)
按指定交叉检查google-service.json的文件路径
启动应用程序(主要活动)时,检查控制台是否有FCM令牌
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.sdev.android.stackoverflow">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name=".firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
主要活动
package in.sdev.android.stackoverflow;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
public class MainActivity extends AppCompatActivity {
public static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "FCM token: " + refreshedToken);
}
}
Firebase软件包示例
<强> MyFirebaseInstanceIDService.java 强>
package in.sdev.android.stackoverflow.firebase;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import static android.content.ContentValues.TAG;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
public void sendRegistrationToServer(String token){
Log.v("FirebaseService", "Token " + token);
}
}
<强> MyFirebaseMessagingService.java 强>
package in.sdev.android.stackoverflow.firebase;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
}
希望这有帮助
答案 4 :(得分:0)
更新
扩展FirebaseMessagingService
使用onNewToken
代替onTokenRefresh
getToken()
方法现已弃用
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);
}
如果要在活动中检索令牌
使用下面的代码.getInstanceId()
代替.getToken()
方法
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String newToken = instanceIdResult.getToken();
Log.e("newToken",newToken);
}
});