zo0r / react-native-push-notification

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

Local Notifications not working in android #2310

Open ragsav opened 1 year ago

ragsav commented 1 year ago

I have been trying the Scheduled local notifications but not a single notification is being triggered. I am not able to find exactly where I am doing mistake.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.notes">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<application
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@drawable/ic_launcher"
  android:roundIcon="@drawable/ic_launcher_round"
  android:allowBackup="false"
  android:theme="@style/BootTheme">

  <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" android:exported="false"/>
  <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" android:exported="false"/>
  <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver" android:exported="false">
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          <action android:name="android.intent.action.QUICKBOOT_POWERON" />
          <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
      </intent-filter>
  </receiver>

  <service
      android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
      android:exported="false" >
      <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
      </intent-filter>
  </service>

  <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
              android:value="false"/>
  <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
              android:resource="@android:color/white"/>

  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
    android:launchMode="singleTask"
    android:windowSoftInputMode="adjustResize"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>

</application>

import PushNotification, {Importance} from 'react-native-push-notification'; import {CONSTANTS} from '../../constants';

export default class NotificationService { static configure() {} static createChannel() { PushNotification.channelExists( CONSTANTS.NOTIFICATION_CHANNEL_ID, exists => { if (exists) { console.log( createChannel ${CONSTANTS.NOTIFICATION_CHANNEL_ID} already exists, ); } else { PushNotification.createChannel( { channelId: CONSTANTS.NOTIFICATION_CHANNEL_ID, channelName: 'Notes channel', channelDescription: 'Notes notification channel', playSound: true, soundName: 'default', importance: Importance.HIGH, vibrate: true, }, created => console.log(createChannel returned '${created}'), ); } }, ); } static getAllScheduledNotifications() { PushNotification.getScheduledLocalNotifications(notifications => { console.log(notifications); }); } static scheduleNotification({notificationID, title, description, timestamp}) { PushNotification.localNotificationSchedule({ date: new Date(timestamp), channelId: CONSTANTS.NOTIFICATION_CHANNEL_ID, id: notificationID, vibrate: true, vibration: 300, title: title, message: description, playSound: true, soundName: 'default', }); } static cancelNotification(notificationID) { PushNotification.cancelLocalNotification(notificationID); } }

Is there any thing more which we need to change?

roma-hladilka commented 1 year ago

@ragsav I also had this issue and I fixed it with additional permission. You need to remove from manifest <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> and add <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

ragsav commented 1 year ago

@roma-hladilka Still no notifications. Also I am not able to receive normal notifications.

renanbronchart commented 1 year ago

I have the same problem only on Android. On iOS everything works fine.

3 months ago, it worked on Android, but now, it doesn't work. I don't know why.

I have the latest version 8.1.0

I tried to change android.permission.SYSTEM_ALERT_WINDOW to android:name="android.permission.SCHEDULE_EXACT_ALARM" but it doesn't work.

ahsan-K commented 1 year ago

same issue i am also facing..

ysshir commented 1 year ago

I was struggling simular situation.

and I found that PushNotification.configure must be outside the component. with IOS, it can be called if its in App. but with Android, it's not called.

in example app, the configure is called in App. so it can recieve notification in any status with IOS. but not in inactive(=killed) with Android.

And also you can put breakpoint in index.js. even it stopped after you killed app, and recieve the notifcation.

tranvantai141 commented 1 year ago

same issue, can't push local noti on android. IOS working fine both remote and local.

LightKnight3r commented 1 year ago

is there anyone find solution for this issue?

Sergey-lang commented 1 year ago

Work at first times, but then not. I see that I received push, but it work and show only if app is closed(background). if reinstall package it work again. What's wrong?

Parkjunwu commented 1 year ago

For me, delete id on PushNotification.localNotificationSchedule then works well. https://github.com/zo0r/react-native-push-notification/issues/1707#issuecomment-923696626

ajalasegun1 commented 1 year ago

If you are using id in the notification for android, it has to be a string of numbers and not letters or a mixture of letters and numbers. You could use just a number for the ID and it would work on the android but you wouldn't be able to use cancelLocalNotification because it requires a string of number. Also ensure that your configuration is in the index file outside of any component.

Thanks @Parkjunwu, your suggestion helped me narrow the problem down to being the ID in my case. I am now able to schedule notification.

MorganTrudeau commented 1 year ago

I found local notifications were not showing on android after targeting sdk version >30. They changed PendingIntent in Android 12

This patch fixed it for me.

diff --git a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
index ad3527b..447a540 100644
--- a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -455,8 +455,13 @@ public class RNPushNotificationHelper {

             int notificationID = Integer.parseInt(notificationIdString);

-            PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent,
-                    PendingIntent.FLAG_UPDATE_CURRENT);
+            PendingIntent pendingIntent = null;
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
+                pendingIntent = PendingIntent.getActivity(context, notificationID, intent, PendingIntent.FLAG_MUTABLE);
+            }
+            else {
+                pendingIntent = PendingIntent.getActivity(context, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+            }

             NotificationManager notificationManager = notificationManager();
infiniteline commented 1 year ago

Hmmm... I'm having the same problem as @renanbronchart where iOS local notifications are fine but Android builds (SDK 31) are not showing notifications. I can alter and view scheduled notifications, they just don't fire. I did the following:

Still nothing. Can someone send some ideas my way?

revosambat commented 1 year ago

@MorganTrudeau After adding patch for react-native-push-notification, moving .configure into index.js and changing id to numerical, localNotifcation() gets triggers but localNotificationSchedule() still doesn't get tigger. I also checked getScheduledLocalNotification(), notification is scheduled but no luck on triggering scheduled notification.

imranshadgoally commented 9 months ago

@infiniteline @revosambat Try with adding channelId and channelName like this channelId: 'default-channel', // (required) channelName: 'Default Channel', // (required)

i.e

PushNotification.localNotificationSchedule({ channelId: 'default-channel', // (required) channelName: 'Default Channel', // (required) title, message, date: fireDate, });

KhaledElOrbany commented 9 months ago

@ragsav If you are using Android 12 and above you may need to consider adding <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> to your AndroidManifest.xml. And check if user has given permission to notificaitons. You may want to ask user to give permission to use them.

image

simonegosetto commented 9 months ago

same problem on Android 13

v4yne1 commented 7 months ago

For Android 13, 14 using RN < 71.0, here is the fix:

https://dev.to/gautham495/asking-notification-permission-in-android-13-for-a-react-native-application-35n2

anmolg27 commented 5 months ago

I found local notifications were not showing on android after targeting sdk version >30. They changed PendingIntent in Android 12

This patch fixed it for me.

diff --git a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
index ad3527b..447a540 100644
--- a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -455,8 +455,13 @@ public class RNPushNotificationHelper {

             int notificationID = Integer.parseInt(notificationIdString);

-            PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent,
-                    PendingIntent.FLAG_UPDATE_CURRENT);
+            PendingIntent pendingIntent = null;
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
+                pendingIntent = PendingIntent.getActivity(context, notificationID, intent, PendingIntent.FLAG_MUTABLE);
+            }
+            else {
+                pendingIntent = PendingIntent.getActivity(context, notificationID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+            }

             NotificationManager notificationManager = notificationManager();

This fixed my issue of local notifications not getting scheduled at a specific time, but you also have to add these permissions in AndroidManifest.xml file on devices with an Android version of at least 13: <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> However, it's important to note that using these permissions comes with a risk. App stores may reject your app because these permissions are potent, and there are policies in place for their careful use. An audit may lead to app removal if misuse is detected. Generally, such permissions are intended for clock or reminder apps. While you can hope that your app won't be rejected, it's advisable to consider an alternative approach: sending notifications remotely instead of scheduling them locally on your device. For more information, refer to this link