在Expo中,推送令牌是用于向设备发送推送通知的重要标识符。在Android独立应用程序中,Expo推送令牌可能没有被正确创建,导致推送通知无法正常工作。
以下是在Android独立应用程序中创建Expo推送令牌的示例代码:
implementation 'com.google.android.gms:play-services-gcm:12.0.1'
implementation 'com.google.firebase:firebase-messaging:12.0.1'
import android.content.Context;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesUtil;
import io.expo.client.BuildConfig;
import io.expo.client.ExpoAndroidNotificationsDelegate;
import io.expo.client.ExponentManifest;
import io.expo.client.kernel.KernelConstants;
import versioned.host.exp.exponent.Constants;
import versioned.host.exp.exponent.ExponentApplication;
public class PushNotification {
public static void register(Context context) {
if (!ExpoAndroidNotificationsDelegate.isInitialized()) {
ExponentApplication application = (ExponentApplication) context.getApplicationContext();
ExponentManifest manifest = application.getManifest();
String experienceId = manifest.optString(ExponentManifest.MANIFEST_ID_KEY);
if (experienceId != null && experienceId.length() > 0) {
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
ExpoAndroidNotificationsDelegate.init(application, experienceId, manifest);
} else {
// Handle error
}
} else {
// Handle error
}
}
}
public static void unregister(Context context) {
if (ExpoAndroidNotificationsDelegate.isInitialized()) {
ExpoAndroidNotificationsDelegate delegate = ExpoAndroidNotificationsDelegate.getInstance();
if (delegate != null) {
delegate.stop();
ExpoAndroidNotificationsDelegate.unpinDeviceToken(context);
ExpoAndroidNotificationsDelegate.release();