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?
@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
})
@vitalii can you close the issue if that helped?
Thank you for help
My issue was not requesting permission on iOS. Surprisingly, local notifications require permissions as well.
Any solution? having the same issue...
Most helpful comment
@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 })