Amine Rahal
Amine Rahal

Reputation: 37

Best and easiest way to create a push notification

I'm using nodejs as server, mongodb as database, mongoose as library and react native as client side . I need to send a notification to a specifique user from the database . How can this be resolved?

Upvotes: 1

Views: 707

Answers (2)

XplosiVe06
XplosiVe06

Reputation: 646

A bit late, but for those coming here and using RealmDB/MongoDB, you can use react-native-push-notification.

You have to create a new Firebase account and connect it with push notifications in your Realm console.

It's all a bit tricky, but when your done with that, you will end up with a code that will look like this in your index.js:

import PushNotificationIOS from '@react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';

PushNotification.configure({
  onRegister: function (token) {
    console.log('TOKEN:', token);
  },

  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification);
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  onAction: function (notification) {
    console.log('ACTION:', notification.action);
    console.log('NOTIFICATION:', notification);
  },

  onRegistrationError: function (err) {
    console.error(err.message, err);
  },

  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },
  popInitialNotification: true,
  requestPermissions: true,
});

PushNotification.subscribeToTopic('topic-id-in-realm-console');

The most important part to make it works with Realm is this line:

PushNotification.subscribeToTopic('topic-id-in-realm-console');

You'll have to add the same topic name in your new notification in Realm:

enter image description here

It's working now on Android (not tested yet on IOS):

Notification on client

To customise your notification you'll have to use rules and functions, but I'm still working on it and don't now yet how to do it.

Hope this will help some lost people like me.

Upvotes: 1

Charuka Herath
Charuka Herath

Reputation: 328

The best method which I used so far is using firebase's real-time DB. I also had the same stack as you. However, I used Firebase and made my life easier.

Upvotes: 1

Related Questions