React-native-push-notification: Local notification not working

Created on 10 Sep 2019  路  2Comments  路  Source: zo0r/react-native-push-notification

I have setup the app for react-native 0.60.5 and with latest version of react-native-push-notification. I have also followed all steps given here - https://github.com/react-native-community/react-native-push-notification-ios#appdelegatem

But when I send local notification, onNotification gets called with notification (I can see log from console.log('NOTIFICATION:', notification); getting printed in consol) object, but I never really receive actual notification on emulator or device. Any clue what I might be doing wrong?

import React, {Fragment} from 'react';
import {SafeAreaView, Button, StatusBar} from 'react-native';

var PushNotification = require('react-native-push-notification');

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

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: 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 App = () => {
  return (
    <Fragment>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <Button
          onPress={() => {
            PushNotification.localNotification({
              title: 'My Notification Title',
              message: 'My Notification Message',
            });
          }}
          title="Press me"
        />
      </SafeAreaView>
    </Fragment>
  );
};

export default App;

Most helpful comment

on ios to show PushNotification.localNotification while the app is in foreground you need to
configure userNotificationCenter too in AppDelegete.m,

#import <UserNotifications/UserNotifications.h>
...

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
...

// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

see:
https://gist.github.com/lutzk/2e2319d112440348619f4b9b25ae6342

i hope it helps

All 2 comments

on ios to show PushNotification.localNotification while the app is in foreground you need to
configure userNotificationCenter too in AppDelegete.m,

#import <UserNotifications/UserNotifications.h>
...

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
...

// Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

see:
https://gist.github.com/lutzk/2e2319d112440348619f4b9b25ae6342

i hope it helps

Thanks alot @lutzk :D

Was this page helpful?
0 / 5 - 0 ratings