Can I use Apple Push Notification Service to have my app do something without notifying user in iOS 7

后端 未结 3 2087
醉梦人生
醉梦人生 2021-01-22 15:23

My Algorithm wishes to work this way-

If some desired event occurs at my server then I wish to send a small amount of data to every user my app (foreground or background

3条回答
  •  花落未央
    2021-01-22 15:43

    You can send "empty" push notification (no text, no sound, no icon). This kind of notifications is allowed and can be used for synchronization with server (to avoid the expensive repetitive polling from the app side).

    If your app is not running then such a notification won't show up among springboard notifications.

    If it is running then it will receive notification - you just have to make sure you don't show the empty ones (if you are also using notifications with actual contents). Your iOS code would look a bit like:

    - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
    {
        NSString *message = nil;
        id alert = [userInfo objectForKey:@"aps"];
    
        if ([alert isKindOfClass:[NSString class]])
        {
            message = alert;
        }
        else if ([alert isKindOfClass:[NSDictionary class]])
        {
            message = [alert objectForKey:@"alert"];
        }
    
        if (message)
        {
            if (![message isEqualToString:@""])
            {
                UIAlertView *alertView = [[UIAlertView alloc]
                                          initWithTitle: @"notification"
                                          message: message
                                          delegate: nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
                [alertView show];
            }
        }
        else
        {
          //this was an empty notification - here you can enqueue
          //your server-synchronization routine - asynchronously if possible
        }
    }
    

    If you don't intend to use any "real" notifications you don't even have to bother with decoding the message.

    If you use ApnsPHP your message generation code would look like this:

    $message = new ApnsPHP_Message($device_apns_token);
    $message->setText('');
    $message->setSound('');
    $message->setExpiry(3600);
    $push->add($message);   
    

提交回复
热议问题