Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用firebase(FCM)生成令牌并共享_Java_Android_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Java 如何使用firebase(FCM)生成令牌并共享

Java 如何使用firebase(FCM)生成令牌并共享,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我正在尝试构建一个应用程序,但当我想用Firebase生成令牌时,我遇到了一个问题,因为在过去,我使用了Google提供的一个旧方法,但使用新方法我无法生成令牌并共享 以前对我有效的方法是发送令牌,现在已弃用的方法如下: import android.util.Log; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceIdService; publ

我正在尝试构建一个应用程序,但当我想用Firebase生成令牌时,我遇到了一个问题,因为在过去,我使用了Google提供的一个旧方法,但使用新方法我无法生成令牌并共享

以前对我有效的方法是发送令牌,现在已弃用的方法如下:

import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        storeToken(refreshedToken);
    }

    private void storeToken(String token) {
        //saving the token on shared preferences
        SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
    }
}
这是一种新方法,但它不起作用。我的令牌未生成,无法共享:

import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseInstanceIDService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        Log.d("NEW_TOKEN",token );
        storeToken(token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

    }

    private void storeToken(String token) {
        //saving the token on shared preferences
        SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token);
    }
}
我的AndroidManifest文件:

<application
        android:usesCleartextTraffic = "true"
        android:name=".app.AppController"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:fullBackupContent="@xml/backup_descriptor">
        <activity android:name=".ge_inicio"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>


感谢您的帮助

为什么不尝试这种方法来生成令牌,只需在MainActivity中的代码下方

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(this, 
instanceIdResult -> {
String newToken = instanceIdResult.getToken();
SharedPreference.getInstance(getApplicationContext()).saveDeviceToken(token)

在AndroidManifest中仅使用此代码

    <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>


Firebase Id在新安装应用程序时创建一次,或在清除应用程序缓存内存时创建Id。您需要在调试之前卸载并重新安装它

很抱歉,我无法使用这些示例构建片段代码,我尝试了任何方法,但我无法使其正常工作:(
<service android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>