22
votes

I have made a sample PushTest app for Push Notification using this tutorial.

And using above tutorial I got the message that 'PushTest' would like to send you Push Notification (exactly once) and after that I delete the app from iPhone, restart the iPhone but unable to get the same message again.

I have run the script sample.php (updating the changes suggested) and got the message 'connected to APNS' & 'your message send'.

But I didn't receive any single push notification.

Please guide me where I am wrong?
Or what shod I try for push notification receive.

8
NOTE, be aware of the BATTERY GOTCHYA ... read carefully here developer.apple.com/library/archive/technotes/tn2265/… at "If the energy or data budget for the device has been exceeded...." - Fattie

8 Answers

30
votes

You will not receive Push only in 2 cases

1.) If your application is in foreground.

2.) you device token is not valid for receiving the push notification please check both the condition if you still do not receive push please let me know. Thanks

6
votes

Make sure you are using push notification enabled provisioning profile. and then check if you are sending token to server.

5
votes

BE AWARE OF THE BATTERY ISSUE, WHICH BLOCKS YOU FOR ONE DAY OR MORE

Never start testing pushes in development mode (tethered phone) unless the phone is FULLY CHARGED.

https://developer.apple.com/library/archive/technotes/tn2265/_index.html

If your notification payload contains the content-available key, your app will receive the notification if iOS or OS X determines it is energy-efficient to do so. If the energy or data budget for the device has been exceeded, your app will not receive any more notifications with the content-available key until the budget has been reset. This occurs once a day and cannot be changed by user or developer action.

In general, notifications (particularly when you are tethered) are incredibly flakey regarding battery state of the phone. (Also, some of the stuff mentioned in that article, which is very old, is plain wrong.) Basically you often JUST WILL NEVER GET notifications if your phones enter "don't get notifications because of battery" mode - and it can go on for days.

Notifications do (or did) prefer cellular WTF!!!

On iOS, push notifications use the cellular data network whenever possible, even if the device is currently using Wi-Fi for other network activity such as web browsing or email. However, the push service will fall back to Wi-Fi if cellular data service isn't available.

This has changed a lot with different iOS versions but it's a huge hassle.

>>>>> !!! CRITICAL TIP !!! <<<<<

Don't forget that:

  1. Build to a tethered phone. You will only get the "sandbox, development" push notifications. BUT THOSE BARELY WORK - THEY ARRIVE VERY LATE, DO NOT WORK IN SOME REGIONS, AND SO ON. THEY ARE SO FLAKEY THEY ARE USUALLY NOT WORTH TRYING.

  2. Increase build number, build normally for Test Flight, send to test flight, and then install from Test Flight. You will now get CORRECT, NORMAL APPLE PUSH NOTIFICATIONS.

Very confusingly you !!DO!! get REAL push notifications on development Test Flight builds.

That is to say - when you build to TestFlight you DO GET exactly the same "real" push notifications as for actual apps published on the app store and used by your public users - real, true, APNS push notifications.

The incredibly confusingly-named "development, sandbox" push notifications are ONLY for "tethered phone builds". (AND, to add confusion on confusion, the "development, sandbox" push notifications are very unreliable, so you never really know if they are working or not.)

Read more here! https://stackoverflow.com/a/60550859/294884

2
votes

I have followed the same tutorial.

Make sure your app is listed in notification center, and it's alert type is anything but not none.

You need to check your notification in 3 conditions,

When your app is open, in background, and when closed.

For that, you need to in check these methods

// To Register your device token
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

//If your app is not able to register
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{}

//Your app receives push notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    UIApplicationState state = [application applicationState];

    // If your app is running
    if (state == UIApplicationStateActive)
    {

        //You need to customize your alert by yourself for this situation. For ex,
        NSString *cancelTitle = @"Close";
        NSString *showTitle = @"Get Photos";
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:cancelTitle
                                                  otherButtonTitles:showTitle, nil];
        [alertView show];
        [alertView release];

    }
    // If your app was in in active state
    else if (state == UIApplicationStateInactive)
    {
    }
}
1
votes

Please check in your device's Settings if notifications for the application are on and ensure that the type of the notification is not 'None' in Notification Center.

1
votes

Make sure, that you select right provision profile provision profile

and that your app is minimized. If app's not minimized use

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"info %@", userInfo);
}

Other useful information about issues.

For TESTING pushes you can use APN Tester Free. Attention: insert token WITH spaces.

0
votes

I was not getting anything in:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any])

But my compiler stopped on breakpoint in

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
0
votes

I ran into a similar issue. Everything was done correctly in the code. I was running the app with a development profile and push notifications on the server (iOS) pointing to production. Make sure your provisioning profile matches the server setting(Sandbox vs. Production). You can also make sure your device is registering correctly.