Praise codes
Praise codes

Reputation: 1

Scheduled Notifications don't work when I pass the id attribute with uuidv4()

Please I would love you all to assist me solve this bug I've been on since last night with react-native-push-notification. I've been trying to send a push notification using the PushNotification.localNotificationSchedule() function. The code works perfectly until I add the id attribute to the notification object. Here's an example of when the code works:

PushNotification.localNotificationSchedule({
        channelId: "channel-id",
        title: `Hi there ${fullName}!`,
        message: `You've Got A To-Do "${todoInfo.title}" Now!`,
        date: new Date(todoInfo.dateDue),
        allowWhileIdle: true,
      });

It also works when I do this:

PushNotification.localNotificationSchedule({
        channelId: "channel-id",
        title: `Hi there ${fullName}!`,
        message: `You've Got A To-Do "${todoInfo.title}" Now!`,
        date: new Date(todoInfo.dateDue),
        id: 555,
        allowWhileIdle: true,
      });

But doesn't work when I use a library to generate a unique id so that I can cancel the notification afterwards; example code here:

import 'react-native-get-random-values';
import { v4 as uuidv4 } from 'uuid';

// Some other code here

PushNotification.localNotificationSchedule({
        channelId: "channel-id",
        title: `Hi there ${fullName}!`,
        message: `You've Got A To-Do "${todoInfo.title}" Now!`,
        date: new Date(todoInfo.dateDue),
        id: uuidv4(),
        allowWhileIdle: true,
      });

Please, is there a way I can get past this?

I tried using a random id generator like uuid and it didn't work. When I pass the id hardcoded, it works. I'm expecting a notification to show up on my app.

Upvotes: 0

Views: 185

Answers (1)

Rayyan
Rayyan

Reputation: 37

As per the documentation, the notification won't work until the Channel is created:

"channel-id" is the default created channelId and therefore notifications work when using it in the channelId

Create a local Channel ID:

you have to hard code the notification channel ID in AndroidMainfest.xml

  <meta-data
  android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
  android:value="@string/default_notification_channel_id" />

For remote Firebase Cloud Messenger Notifications refer to FCM documentation

Checking Channel Exists & Listing them

Here is the code from the documentation to check for channels & list out the available channels for notifications

PushNotification.getChannels(function (channel_ids) {
  console.log(channel_ids); // ['channel_id_1']
});

Take note that channel ID is not the same as a unique-id of the notification, so trying to use it with uuid is not it's supposed usage

Upvotes: 1

Related Questions