[英]Push notification when app in background
Suppose a scenario: there are 2 users User A and User B. User A sends a push notification to User B but at that time the app was in background at User B's side. 假设有一个场景:用户A和用户B有2个用户。用户A向用户B发送推送通知,但那时该应用程序在用户B一侧处于后台。 Now the problem is User B does not receive notification because the app was in background but receives as soon as the app is bought to foreground again.
现在的问题是,用户B由于该应用程序处于后台状态而没有收到通知,但是一旦该应用程序再次被购买到前台,它就会收到通知。 Is there any way around which enables User B to get notification in the background state as well.
是否有任何方法可以使用户B在后台状态下获得通知。 Also if the Alert Style for the application is configured by the user to be "None" in the Settings app of the device, could this prevent him from receiving the notifications when app is in background.?
另外,如果用户在设备的“设置”应用程序中将应用程序的警报样式配置为“无”,是否可以防止他在应用程序处于后台时收到通知?
Thanks in advance!! 提前致谢!!
If the user disables alert notifications , he won't receive such alerts . 如果用户禁用警报通知,他将不会收到此类警报。 Otherwise he will receive the alert immediately even when the app is not running .
否则,即使该应用未运行,他也将立即收到警报。
what I understand from your question you want to run some code when notification received in background mode... 我从您的问题中了解到,您想在后台模式下收到通知时运行一些代码...
first in your target menu->capabilities ->on the background mode 首先在目标菜单中->功能->在后台模式下
then inside your AppDelegate add your code under the method which will fire when app is in background mode 然后在您的AppDelegate内部,将方法下的代码添加到当应用程序处于后台模式时将触发的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {}
**But remember the most important thing ** **但请记住最重要的事情**
your push notification structure must contain content_avialable = 1 您的推送通知结构必须包含content_avialable = 1
example: 例:
{
aps = {
alert = {
body = "new message";
title = "I";
};
"content_available" = 1;
};
"click_action" = "GOTO_M";
data = {
};
"gcm.message_id" = ;
"google.c.a.e" = 1;
}
This is really what push notifications are about. 这实际上就是推送通知的内容。 If you allow for the app to send you push notifications, you will receive them even when the app is in the background.
如果允许该应用程序向您发送推送通知,则即使该应用程序在后台,您也将收到它们。 I suggest you read up on how to send push notifications.
我建议您阅读有关如何发送推送通知的内容。
Another option is to let the app itself display the notification, this can still be done while the app is in the background. 另一个选择是让应用程序本身显示通知,当应用程序在后台时仍可以完成此操作。
When they set the Alert style to none and turn of notifications they will not recieve the push notifications anymore. 当他们将“警报”样式设置为“无”并打开通知时,他们将不再接收推送通知。 But then again, they shouldn't get any if they don't want to.
但是话又说回来,如果他们不想的话,他们什么也得不到。
To your second question: 关于第二个问题:
if the Alert Style for the application is configured by the user to be "None" in the Settings app of the device, could this prevent him from receiving the notifications when app is in background?
如果用户在设备的“设置”应用中将应用的警报样式配置为“无”,是否可以防止他在后台运行时收到通知?
If a user has set "None" as the Alert Style then notifications will be received but not shown. 如果用户将“无”设置为警报样式,则将收到通知,但不会显示。 However, the user can still see that push message in the notification center.
但是,用户仍然可以在通知中心看到该推送消息。 That is the phone will not display alerts on the screen.
也就是说,电话不会在屏幕上显示警报。
Now to your first question, when properly implemented Push Notifications will be received even when the app is in background. 现在,您的第一个问题是,即使应用程序处于后台,也将在正确实施推送通知时收到通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.