【发布时间】:2016-09-18 08:55:07
【问题描述】:
这是在我的 MainActivity 中用作 BroadcastReceiver 的代码
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
//When the broadcast received
//We are sending the broadcast from GCMRegistrationIntentService
@Override
public void onReceive(Context context, Intent intent) {
//If the broadcast has received with success
//that means device is registered successfully
if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
//Getting the registration token from the intent
String token = intent.getStringExtra("token");
//Displaying the token as toast
Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show();
//if the intent is not with success then displaying error messages
} else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
Toast.makeText(getApplicationContext(), "GCM registration error!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
}
}
};
我学习了这个关于 GCM 的教程
https://www.simplifiedcoding.net/android-push-notification-using-gcm-tutorial/
【问题讨论】:
-
我认为通过滑出关闭应用程序是强制关闭应用程序,因此除非用户再次启动应用程序,否则它不会收到 GCM 消息。请看我之前的回答SO question
-
我是用 WakefulBroadcastReceiver 做到的
-
无论您的应用程序是打开还是关闭,您都可以接收 GCM(现为 FCM)消息。我建议不要编写自己的 WakefulBroadcastReceiver,请使用提供的 GcmListenerService 或更容易使用的 FirebaseMessagingService。查看示例:github.com/firebase/quickstart-android/tree/master/messaging
-
@acer 你是怎么解决的?
标签: android android-studio push-notification firebase-cloud-messaging google-cloud-messaging