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.

Local notification is not showing for iOS simulator

See original GitHub issue

Env:

  React Native Environment Info:
    System:
      OS: macOS 10.14.2
      CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
      Memory: 80.06 MB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 10.4.0 - ~/.nvm/versions/node/v10.4.0/bin/node
      Yarn: 1.7.0 - ~/.nvm/versions/node/v10.4.0/bin/yarn
      npm: 6.7.0 - ~/.nvm/versions/node/v10.4.0/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 27, 28
        Build Tools: 27.0.3, 28.0.3
        System Images: android-25 | Google APIs ARM 64 v8a, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
    IDEs:
      Android Studio: 3.3 AI-182.5107.16.33.5199772
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.6.3 => 16.6.3
      react-native: 0.58.1 => 0.58.1
    npmGlobalPackages:
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7

So I have very basic piece of code

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button, Alert, TextInput, PushNotificationIOS} from 'react-native';
import PushNotification from 'react-native-push-notification';


const configure = {
  // (required) Called when a remote or local notification is opened or received
  onNotification: function(notification) {
    console.log( 'NOTIFICATION:', notification );

    // process the notification

    // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  //  notification.finish('backgroundFetchResultNewData');
  },

  // ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
  senderID: "YOUR GCM (OR FCM) SENDER ID",

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

  /**
   * (optional) default: true
   * - Specified if permissions (ios) and token (android and ios) will requested or not,
   * - if not, you must call PushNotificationsHandler.requestPermissions() later
   */
  requestPermissions: true,
};

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

export default class App extends Component {
  constructor(props) {
    super(props);
    PushNotification.configure(configure);
  }
  render() {
    return (
      <View style={styles.container}>
        <Button
          title={'call push notification'}
          onPress={() => {
            PushNotification.localNotification({
              message: 'my first PN',
              userInfo: { id: '123' }
            });
        }} />
        <Text style={styles.welcome}>Welcome to React Native 0.58!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({});

when I press on a button I can see console.log from onNotification

NOTIFICATION: 
{foreground: true, userInteraction: false, message: "my first PN", data: {…}, badge: 0, …}
alert: "my first PN"
badge: 0
data: {id: "123"}
finish: ƒ finish(res)
foreground: true
message: "my first PN"
sound: "default"
userInteraction: false
__proto__: Object

the callback whis is fired whe notification is closed. But I can’t see on on the screen. I’m using iPhone X 12.0 simulator Any ideas what could be wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
trspechtcommented, Feb 26, 2019

@vitalii on iOS you can’t get notifications when the app is in foreground, so maybe that’s the issue here. I’d suggest you try to send a scheduled notification after a few seconds so you have time to close the app after it’s triggered.

PushNotification.localNotificationSchedule({ message: "message", date: new Date(Date.now() + (20 * 1000)) //20 seconds here })

1reaction
vitaliicommented, Mar 18, 2019

Thank you for help

Read more comments on GitHub >

github_iconTop Results From Across the Web

iphone local notification in simulator - Stack Overflow
Yes, local notifications work with the simulator. However, make sure you are implementing application:didreceiveLocalNotification in your app delegate if ...
Read more >
Local Notification not working in Background Mode
Everything works fine in simulator, i can see notifications are displayed in foreground as well as background mode. But when i run the...
Read more >
How to Send Push Notifications to the iOS Simulator
“Simulator supports simulating remote push notifications, including background content fetch notifications. In Simulator, drag and drop an APNs file onto the ...
Read more >
Quick guide on local notifications for iOS - tanaschita.com
Learn how to schedule local notifications in different ways and how to handle them. ... iOS local and push notifications allow us to...
Read more >
Scheduling local notifications - a free Hacking with iOS
Remote notifications require a server to work, because you send your message to Apple's push notification service (APNS), which then forwards it ...
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