GCM 2.0在Android P中无法工作。

5

我正在 Android P 中实现 GCM。
但是我无法接收来自 GCM 的广播。
在 Android P 中出了什么问题?
顺便说一下,在 Android O 中工作良好。

    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
    registrationIntent.setPackage("com.google.android.gsf");
    registrationIntent.putExtra("sender", sender_id);
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));

public class GCMBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
        String messageType = gcm.getMessageType(intent);

        Log.e("GCM", "action=" + intent.getAction() + " registration_id="+intent.getStringExtra("registration_id"));
    }
}

你使用的是哪个版本的GCM?引用的registrationIntent是否来自库? - OferR
GCM是否被支持?转换到Firebase以适应Android P的新后台进程限制。 - Mohammed Atif
3个回答

4

您必须使用旧版本的GCM。

请升级到GCM 11或更高版本。(最新版本为15.0.1:com.google.android.gms:play-services-gcm:15.0.1),或者更好地迁移到FCM。(GCM现已弃用)


3

根据文档,截至2018年4月10日,Google已经弃用了GCM。GCM服务器和客户端API已被弃用,并且将在2019年4月11日之前删除。请将GCM应用迁移到Firebase Cloud Messaging(FCM),它继承了可靠和可伸缩的GCM基础架构以及许多新功能。请参阅迁移指南以了解更多信息。

因此,您可能(早晚)需要迁移到FCM

最近还推出了Firebase In-App Messaging


0

我通过以下方式使用GCM 3.0自己解决了这个问题:

public void getInstanceIdToken() {
    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

public class RegistrationIntentService extends IntentService {
    private static String TAG = RegistrationIntentService.class.getSimpleName();

    public RegistrationIntentService() {
        super(TAG);
    }

    String senderId = "YourSenderId";

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        InstanceID instanceID = InstanceID.getInstance(this);
        try {
            String token = instanceID.getToken(senderId,
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接