Skip to content

Instantly share code, notes, and snippets.

@npomfret
Created October 8, 2016 14:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save npomfret/2566cc84ada1a807d201b8146e1fa25e to your computer and use it in GitHub Desktop.
Save npomfret/2566cc84ada1a807d201b8146e1fa25e to your computer and use it in GitHub Desktop.
'use strict';
import PushNotification from "react-native-push-notification";
let _pushNotificationToken = null;
const _pushNotificationListeners = [];
function init() {
console.log("PushNotification init");
PushNotification.configure({
onRegister(token) {
console.log('Registered pushNotificationToken:', token);
_pushNotificationToken = token;
},
onNotification(notification) {
console.log("Received pushNotification",);
_pushNotificationListeners.forEach((listener) => {
try {
listener(notification);
} catch (err) {
console.warn("Listener failed to process push notification", err);
}
})
},
senderID: "1068946777489",
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: false,
requestPermissions: true
});
}
init();
export default class PushNotifications {
constructor() {
}
addListener(listener) {
_pushNotificationListeners.push(listener);
return () => {
this._listeners = this._listeners.filter((l) => l !== listener);
}
}
token() {
return _pushNotificationToken;
}
popInitial() {
PushNotification.configure({
popInitialNotification: true,
});
}
}
@jose2007kj
Copy link

jose2007kj commented Aug 8, 2017

sir, thanks for this code ..........................by importing this file now i am able to receive notification received message even when the app is closed(i got notification received message in react-native android-log) but in app its not showing any notification how to correct this? All i did was pasted this code in notifications .js file and imported it to app.js file import {PushNotifications} from './notification.js';

@jose2007kj
Copy link

sir,now working fine.......... problem was incorrect way of send notification when i aded more details in message i got it working

'message' 	=> 'here is a message. message',
	'title'		=> 'This is a title. title',
	'subtitle'	=> 'This is a subtitle. subtitle',
	'tickerText'	=> 'Ticker text here...Ticker text here...Ticker text here',
	'vibrate'	=> 1,
	'sound'		=> 1,
	'largeIcon'	=> 'large_icon',
	'smallIcon'	=> 'small_icon'

@vova-demchuk
Copy link

why do we need popInitial method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment