I'm testing push notifications with content-available=1, and they don't seem to be delivered to the app in the background unless on Wi-Fi.
I have a simple log statement at the beginning of the push notification handler:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {
NSLog(@"Notification received: %@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
Here is my test:
On Wi-Fi, the console log shows the notification. If I go to Settings and turn off Wi-Fi, switching to 4G, notifications no longer appear in the log (although they do slide in at the top of the screen, so I know they are being delivered).
There are no crash logs, and the notification is logged if I manually tap on it. Furthermore, this problem does NOT occur if I am debugging the app in Xcode. (i.e., if I am debugging in Xcode, the app will receive the notification in the background on 4G). Has anyone else experienced this behavior? Or am I doing something wrong?
EDIT: To be specific: according to my tests, if the following conditions are true, then the remote notification delegate method above will not be called:
However if condition 2 is removed (i.e., the phone is connected to Wi-Fi), then the handler will be called.
Go to iOS' settings → "Your app name" → Notifications to ensure notifications are enabled for your app. You will need to restart your app after enabling push notifications. If you find a recent install with a push token in the debug tool, make sure your app is not opened in the foreground during your tests.
Apple announced support for web push notifications for Safari on iOS starting in 2023. At the company's annual developer event WWDC in June 2022, Apple announced that this feature was coming to iOS.
APNS is a cloud-based service that enables apps to send push notifications from a remote server to iOS users through a secure connection. Before iOS users receive your messages, you'll need a p. 12 certificate from Apple, which authorizes push sending through APNS.
Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered—immediately or in the scheduled notification summary.
Try the following code:
// AppDelegate.h
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *DeviceToken;
NSMutableDictionary *App_Messages;
NSString *Longitude,*Latitude;
NSMutableDictionary * badge;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewcontrollervc;
@property (strong, nonatomic) UINavigationController *navcontroller;
@property (nonatomic,retain)NSMutableDictionary *badge;
@property (nonatomic,retain)NSString *DeviceToken;
// AppDelegate.m
#import "ViewController.h"
@implementation AppDelegate
@synthesize badge,DeviceToken;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.viewcontrollervc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
self.navcontroller = [[UINavigationController alloc]initWithRootViewController:self.viewcontrollervc];
self.window.rootViewController = self.navcontroller;
self.navcontroller.navigationBarHidden = YES;
//Notification
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSDictionary * remoteNotificationObj = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (remoteNotificationObj)
{
[self performSelector:@selector(handleRemoteNotificationWithUserInfo:) withObject:remoteNotificationObj afterDelay:3.0];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self handleRemoteNotificationWithUserInfo:userInfo];
}
-(void)handleRemoteNotificationWithUserInfo:(NSDictionary *)userInfo
{
NSLog(@"userInfo - %@",userInfo);
NSDictionary *alertData = [userInfo objectForKey:@"aps"];
NSDictionary *returnDatalert=[alertData objectForKey:@"alert"];
NSString *alertmsg=[returnDatalert objectForKey:@"body"];
NSLog(@"alertmsg %@",alertmsg);
self.badge = [NSMutableDictionary dictionaryWithDictionary:[alertData objectForKey:@"badge"]];
NSString *notificationtype=[badge objectForKey:@"fnct"];
NSLog(@"%@",notificationtype);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);
NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];
self.DeviceToken=dt;
NSLog(@"~~~~devToken(dv)=%@",deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With