简体   繁体   中英

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. 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. Is there any way around which enables User B to get notification in the background state as well. 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

- (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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM