zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.75k stars 2.05k forks source link

local notification being sent but not showing on device. #2073

Open Rhys-Woolcott opened 3 years ago

Rhys-Woolcott commented 3 years ago

Bug

I have sent a local notification but it doesn't show on the device even though my console shows no errors

Environment info

react-native info output:

System:
    OS: Windows 10 10.0.19042
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
    Memory: 5.53 GB / 15.95 GB
  Binaries:
    Node: 16.1.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 7.11.2 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK:
      API Levels: 23, 29, 30
      Build Tools: 29.0.2, 30.0.3, 31.0.0
      System Images: android-30 | Google APIs Intel x86 Atom, android-30 | Google APIs Intel x86 Atom_64, android-30 | Google Play Intel x86 Atom
      Android NDK: Not Found
    Windows SDK:
      AllowDevelopmentWithoutDevLicense: Enabled
      AllowAllTrustedApps: Enabled
      Versions: 10.0.18362.0
  IDEs:
    Android Studio: Not Found
    Visual Studio: Not Found
  Languages:
    Java: 16.0.1
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.1 => 17.0.1
    react-native: ^0.64.2 => 0.64.2
    react-native-windows: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: x.x.x

Steps To Reproduce

import PushNotificationIOS from '@react-native-community/push-notification-ios';
import { string } from 'prop-types';
import PushNotification, { Importance } from 'react-native-push-notification';

const config = async () => {
    PushNotification.configure({
        // (optional) Called when Token is generated (iOS and Android)
        onRegister: function(token) {
            console.log('TOKEN:', token);
        },

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

            // process the notification

            // (required) Called when a remote is received or opened, or local notification is opened
            notification.finish(PushNotificationIOS.FetchResult.NoData);
        },

        // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
        onAction: function(notification) {
            console.log('ACTION:', notification.action);
            console.log('NOTIFICATION:', notification);

            // process the action
        },

        // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
        onRegistrationError: function(err) {
            console.error(err.message, err);
        },

        // 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
         * - if you are not using remote notification or do not have Firebase installed, use this:
         *     requestPermissions: Platform.OS === 'ios'
         */
        requestPermissions: true,
    });

    createChannel();
};

const createChannel = () => {
    PushNotification.createChannel(
        {
            channelId: 'nerdee',
            channelName: 'nerdee',
        },
        (created) => console.log(`Channel created: ${created}`)
    );
};

const sendNotification = (title?: string, body?: string) => {
    PushNotification.scheduleLocalNotification({
        title: title ? title : 'Title',
        message: body ? body : 'Body',

        channelId: 'nerdee',

        date: new Date(Date.now() + 5 * 1000),
    });

    console.log('Sent Notification');
};

export default { config, sendNotification, createChannel };

Describe what you expected to happen:

1. 2.

Reproducible sample code

Notification should send but does not.

output in react native:

 LOG  TOKEN: {"os": "android", "token": "fg1heZcdSruX4mAh2FKWT6:APA91bHsMY72ehina1sojgJ0Pn3XZgYjZDpkRxvKekczo5EOa5Uc5sgPUm2E30fsC5adDvP_xLNlUUYf_uG57uW47kedIPzqDUId5dKQmzTA00sb5NyydRAqzT6MDh0A6xPYbWyjLABP"}
 LOG  Channel created: false
 LOG  Sent Notification
panZongLin commented 3 years ago

the same. It was normal before

Rhys-Woolcott commented 3 years ago

the same. It was normal before

Explain. Your comment makes 0 sense

tavour-frank commented 3 years ago

Also running in to this. I've been able to get a local notification to show by duplicating some keys in a data field, e.x:

function onNotification(notification) {
...
  const { data, id, userInteraction, message } = notification;
  if (!userInteraction) {
    PushNotification.localNotification({
      channelId: ANDROID_CHANNEL_ID,
      ...data,
      ...notification,
      data: { ...data },
    });
  }
...
}

But without the data field, no local notification shows

Rhys-Woolcott commented 3 years ago

Also running in to this. I've been able to get a local notification to show by duplicating some keys in a data field, e.x:

function onNotification(notification) {
...
  const { data, id, userInteraction, message } = notification;
  if (!userInteraction) {
    PushNotification.localNotification({
      channelId: ANDROID_CHANNEL_ID,
      ...data,
      ...notification,
      data: { ...data },
    });
  }
...
}

But without the data field, no local notification shows

I see. That is quite odd. Might try making a pull request later with a fix if i can work one out.

Dallas62 commented 3 years ago

Hi, duplicate the information inside data doesn't change the way it's working. The data field is not used to display the notification.

Rhys-Woolcott commented 3 years ago

Hi, duplicate the information inside data doesn't change the way it's working. The data field is not used to display the notification.

Hmmm. Maybe it just requires a data field but does not say in the docs

Dallas62 commented 3 years ago

You should take a look at your AndroidManifest, you probably disabled foreground notifications.

tavour-frank commented 3 years ago

Following up - after looking through some app logs, seems my issue was on the back-end rather than with the library; our service was not correctly sending along the message field in the payload and was instead setting it in a nested data field

rabindrachhachan commented 3 years ago

@tavour-frank , Can you please share the notification object sent from backend? I am still struggling to fix this. In my case onNotification is not trigger when app is in foreground.