zo0r / react-native-push-notification

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

Scheduled Notifications are not displaying..... #2399

Open sarveshhh-m opened 11 months ago

sarveshhh-m commented 11 months ago

Question

I have used this library react-native-push-notifications in my react-native-app. whenever i schedule any notification, it does not appear(even though it is listed in the getScheduledLocalNotifications() ), PushNotification.localNotification() works fine but the PushNotification.scheduleLocalNotification() does not work even in simpler circumstances.

notifications.android.ts

import PushNotification from 'react-native-push-notification';

const handleScheduledNotification = (
  title: string,
  message: string,
//   date: Date,
) =>
  PushNotification.scheduleLocalNotification({
    channelId: "reminders",
    title: title,
    message: message,
    date: new Date(Date.now() + 5 * 1000),
    importance: 'high',
    vibration: 500,
    repeatTime: 60000,
    repeatType: "minute"
  });

const handleNotification = (title: string, message: string) => PushNotification.localNotification({
  channelId: "reminders",
  title: title,
  message: message
});

export { handleScheduledNotification, handleNotification };

AndroidManifest.xml


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

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

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
        <!-- Change the value to true to enable pop-up for in foreground on receiving remote notifications (for prevent duplicating while showing local notifications set this to false) -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
            android:value="true"/>
        <!-- Change the resource name to your App's accent color - or any other color you want -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
            android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->

        <receiver
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions"
            />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver"
            android:exported="true"

            >
            <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>
      <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>
</manifest>
sumit-do-it commented 6 months ago

It depends on the targetSdkVersion you have set in your build.gradle file inside the android directory. if targetSdkVersion < 33, then you do not need to read this comment further.

In android 14, there are few changes related SCHEDULE_EXACT_ALARM permission. Now permission of SCHEDULE_EXACT_ALARM is by default disabled by android. So setExact method of AlarmManager android API will throw an exception if user does not grant the permission explicitly.

So in your case, react-native-push-notification module is using "setExact" method internally and I think, that's why PushNotification.scheduleLocalNotification() is unable to work.

Robiullah2244 commented 3 months ago

Any solution? How can I show the permission prompt on demand?