question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

iOS - Notifications not showing up when app is in background

See original GitHub issue

I can trigger notifications when the app is in the foreground (i.e. they appear in the dropdown notifications area) but they don’t seem to appear once the app is in the background.

// AppDelegate.m
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import "RCTRootView.h"

#import "RCTPushNotificationManager.h"


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  /**
   * Loading JavaScript code - uncomment the one you want.
   *
   * OPTION 1
   * Load from development server. Start the server from the repository root:
   *
   * $ npm start
   *
   * To run on device, change `localhost` to the IP address of your computer
   * (you can get this by typing `ifconfig` into the terminal and selecting the
   * `inet` value under `en0:`) and make sure your computer and iOS device are
   * on the same Wi-Fi network.
   */

  jsCodeLocation = [NSURL URLWithString:@"http://192.168.1.101:8081/index.ios.bundle?platform=ios&dev=true"];

  /**
   * OPTION 2
   * Load from pre-bundled file on disk. The static bundle is automatically
   * generated by the "Bundle React Native code and images" build step when
   * running the project on an actual device or running the project on the
   * simulator in the "Release" build configuration.
   */

//   jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"olisReactNative"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

// Required to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
}
// Required for the register event.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the notification event.
// https://facebook.github.io/react-native/docs/pushnotificationios.html#content
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
NSLog(@"didReceiveRemoteNotification");
[RCTPushNotificationManager didReceiveRemoteNotification:notification];
}

// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}

@end

    PushNotification.localNotification({
      // No idea why, but seems like I need the Android only properties as well to trigger a notification in the dropdown

      /* Android Only Properties */
      id: 0, // (optional) default: Autogenerated Unique ID
      title: "My Notification Title", // (optional)
      ticker: "My Notification Ticker", // (optional)
      largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
      smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"

      /* iOS and Android properties */
      message: "My Notification Message" // (required)
    });

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
sibeliuscommented, Jul 1, 2016
// fetch notifications in the background and foreground
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification 
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

[RCTPushNotificationManager didReceiveRemoteNotification:notification];
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"Notification Body %@", notification);

}

based on http://stackoverflow.com/a/36750318/2628278

In order for notifications to hit your app in the background you need to also define a fetchCompletionHandler, with a completion handler function like below. The aps:{content-available:1} payload should wake up you application and trigger this code in your AppDelegate, and in turn hit your JavaScript in RN.

0reactions
hyperhcommented, Jan 31, 2017

@npomfret I stopped working on the project which used this library. Feel free to close it if you like.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notifications not working in iOS 16? 9 Ways to fix it! - iGeeksBlog
To fix Notifications not working on iPhone running iOS 16, 1. Turn off Focus on iPhone, 2. Add exceptions to Focus, 3.
Read more >
IOS 14.2 iPhone 10X notifications not pre… - Apple Community
IOS 14.2 iPhone 10X notifications not presented on lock screen/ no background refresh. After upgrading to IOS 14.2 my iPhone X is no...
Read more >
Are my push notifications not available if background app ...
Make sure that the app supports notifications. Go to Settings > Notifications, select the app, and make sure that Notifications are turned on....
Read more >
Push notification is not working when app is in background ...
And make sure Background App refresh is enabled for your app. This is NOT the same with your notifications. Obviously make sure your ......
Read more >
8 Quick Fixes for Notifications Not Working on iPhone - Dr.Fone
1. Simply restart your iPhone 2. Check if your iPhone is in Silent Mode 3. To update your iPhone to latest iOS, Go...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found