问题
I followed the example in the PushSharp repository at: PushSharp Android Client Sample
Everything is working great if the app is open or in the background (by hitting home or back button). If I close the app by swiping it away in the application manager I no longer receive notifications, though.
My assumption was that since the PushHandlerService is marked as a [Service] that it would remain open even when the app is closed and continue to listen for notifications. Is there a way to continue to receive notifications with the app closed, or am I just doing it wrong?
I would include code but what I've got is taken pretty much exactly from the example at the above link.
EDIT:
I tried firing PushClient.Register on app start even through the device is already registered in hopes that this would start the PushHandlerService if it wasn't started. Even with re-registering on each launch of the app I still don't receive notification when the app is closed.
回答1:
Did you tried it with debug, or release mode? I experienced the same when I started my app in Debug mode.
回答2:
This behavior might be device specific. I experienced the same with Huawei devices using EMUI. Could solve it by adjusting the battery saving settings.
See this question: Remote Push Notifications and terminated Apps
回答3:
If you want your service to keep running even if the application is killed then you should add the following in the service tag in your AndroidManifest.xml file.
android:stopWithTask="false"
OR
If you are using GCM for push notification and want to run the service when the notification arrives then you have to register your service as the GCM receiver in your AndroidManifest.xml file. You can do this by adding the following code
<service
android:name=".yourPushService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
来源:https://stackoverflow.com/questions/19459824/android-push-notifications-not-being-received-when-app-closed