我正在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"));
}
}
答案 0 :(得分:4)
您必须使用旧版本的GCM。
升级到GCM 11或更高版本。 (最新版本是15.0.1:com.google.android.gms:play-services-gcm:15.0.1),甚至更好的是,迁移到FCM。 (现已弃用GCM)
答案 1 :(得分:3)
documentation的状态...
自2018年4月10日起,Google已弃用GCM。 GCM服务器和客户端API已被弃用,将于2019年4月11日删除。将GCM应用程序迁移到Firebase Cloud Messaging(FCM),它继承了可靠且可扩展的GCM基础架构以及许多新功能。请参阅migration guide了解更多信息。
因此,您可能(迟早要迁移)到FCM
。
从最近开始,还有Firebase In-App Messaging。
答案 2 :(得分: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();
}
}
}