React Native Push Notifications_ Your Ultimate Guide
React Native Push Notifications_ Your Ultimate Guide
336
8
Hi Geeks !! In this fast-paced world of mobile app
or
name.
haven’t already.
Native project.
SHA-1.
cd android
./gradlew signingReport
After downloading the google-services.json file from the
Firebase services.
buildscript {
ext {
...
buildToolsVersion = "34.0.0"
minSdkVersion = 21
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "25.1.8937393"
kotlinVersion = "1.8.0"
...
repositories {
...
mavenCentral()
...
dependencies {
...
classpath("com.google.gms:google-services:4.4.0") // Google
Service Plugin
...
located at <project>/<app-module>/build.gradle.
...
...
dependencies {
...
implementation(platform("com.google.firebase:firebase-
bom:32.7.1"))
...
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission
android:name="android.permission.POST_NOTIFICATIONS"/>
<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">
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_
foreground"
android:value="true" />
<meta-data
android:name="com.dieam.reactnativepushnotification.channel_creat
e_default"
android:value="true" />
<meta-data
android:name="com.dieam.reactnativepushnotification.notification_
color"
android:resource="@color/white" />
<receiver
android:name="com.dieam.reactnativepushnotification.modules.RNPus
hNotificationActions"
android:exported="true" />
<receiver
android:name="com.dieam.reactnativepushnotification.modules.RNPus
hNotificationPublisher"
android:exported="true" />
<receiver
android:name="com.dieam.reactnativepushnotification.modules.RNPus
hNotificationBootEventReceiver"
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.RNPus
hNotificationListenerService"
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>
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Lastly, go to
does not exist, create it. This file determines the color
<resources>
<color name="white">#FFF</color>
</resources>
Push Notification.
cd android
// Use this command to check for any error or dependency issue
./gradlew clean
receiving notifications.
app.
PushNotification.createChannel(
);
PushNotification.localNotification({
})
};
import {
Button,
SafeAreaView,
ScrollView,
StatusBar,
Text,
useColorScheme,
View,
} from ‘react-native’;
import {
Colors,
} from ‘react-native/Libraries/NewAppScreen’;
import LocalNotification from ‘./Notification’;
const backgroundStyle = {
};
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
alignContent: 'center',
}}>
</View>
</ScrollView>
</SafeAreaView>
);
notification successfully.
file).
node_modules/react-native/Libraries/Permission
sAndroid/PermissionsAndroid.js
...
...
POST_NOTIFICATIONS?: string,
};
const PERMISSION_REQUEST_RESULT = Object.freeze({
...
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS',
});
notifications.
try {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
} catch (error) {
console.error(error)
};
const RemoteNotification = () => {
useEffect(() => {
checkApplicationPermission();
PushNotification.getChannels(function (channel_ids) {
channel_ids.forEach((id) => {
PushNotification.deleteChannel(id)
})
});
PushNotification.configure({
// (optional) Called when Token is generated (iOS and
Android)
console.log('TOKEN:', token);
},
},
);
PushNotification.localNotification({
channelId: key, //this must be same with
channelId in createchannel
title: strTitle,
message: strBody,
});
},
senderID: '1234567890'
popInitialNotification: true,
requestPermissions: true,
});
}, []);
return null;
};
1. Foreground State
2. Background State
3. Quit State
App.tsx.
In App.tsx we are going import RemoteNotification and
render it.
import {
Button,
SafeAreaView,
ScrollView,
StatusBar,
Text,
useColorScheme,
View,
} from 'react-native';
import {
Colors,
} from 'react-native/Libraries/NewAppScreen';
const backgroundStyle = {
};
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
backgroundColor={backgroundStyle.backgroundColor}
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
alignContent: 'center',
}}>
<RemoteNotification />
</View>
</ScrollView>
</SafeAreaView>
);
}
export default App;
in your projects.