Open Ruzen1 opened 3 years ago
Hi,
When do you want to call onNotification
? On app open ? When a remote notification arrives ?
What is the payload of your remote notification ?
All information exist in issues history.
Regards
Here is my notification service class:
import { Platform } from 'react-native';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';
import emitter from 'tiny-emitter/instance';
import { ANDROID_SENDER_ID } from '../../shared/settings/Constants';
import processNotification from '../helpers/PushNotificationHelper';
let hasStarted = false;
let pushToken = null;
let badgeNumber = 0;
export const getPushToken = () => pushToken;
export const getBadgeNumber = () => badgeNumber;
export const startNotifications = () => {
PushNotification.configure({
onRegister: (token) => {
if (hasStarted) return;
console.log('TOKEN:', token);
pushToken = token;
emitter.emit('push-notification-token-updated', token);
hasStarted = true;
},
onNotification: (notification) => {
console.log('notification: ', notification);
// Normalise the notification across iOS and Android
if (!notification) return;
// TODO: this processing might be incomplete....
// TODO: URL decode on the text?
const notificationData = processNotification(Platform.OS, notification);
console.log('notificationData: ', notificationData);
emitter.emit('push-notification-received', notificationData);
if (notification.badge) {
badgeNumber = notification.badge;
console.log('badgeNumber: ', badgeNumber);
emitter.emit('push-notification-badge-changed', badgeNumber);
PushNotification.setApplicationIconBadgeNumber(badgeNumber);
}
// Avoid being throttled on iOS by notifying the system that
// we're done with a notification
if (Platform.OS === 'ios'
&& notification
&& notification.finish
&& typeof notification.finish === 'function'
) {
if (
notification
&& PushNotificationIOS
&& PushNotificationIOS.FetchResult
&& PushNotificationIOS.FetchResult.NoData
) {
notification.finish(PushNotificationIOS.FetchResult.NoData);
}
}
},
senderID: ANDROID_SENDER_ID,
popInitialNotification: false,
requestPermissions: true,
});
};
I call the startNotifications(); function from app.js file. I am successfully getting the console.log('TOKEN:', token);
log but not the console.log('notification: ', notification);
log when the notification is supposed to be received. This problem exists in both iOS and Android platforms and we have checked our push notifications server to confirm that it is sending the notifications successfully.
I am using react-native-push-notification version 7.3.1
.
Can you share the remote notification payload, without it I can't help.
Yep
{
"uuid":"XXXX-XXX-XX-XXXX-XXXXXXXXXX",
"displayName":"App\\Jobs\\SendPush",
"job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":3,"maxExceptions":null,"delay":null,"timeout":null,"timeoutAt":null,
"data":{
"commandName":"App\\Jobs\\SendPush",
"command":"O:17:\"App\\Jobs\\SendPush\":15:{s:5:\"tries\";i:3;s:9:\"\u0000*\u0000toUser\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:15:\"App\\Models\\User\";s:2:\"id\";i:4907;s:9:\"relations\";a:0:{}s:10:\"connection\";s:5:\"pgsql\";}s:10:\"\u0000*\u0000subject\";s:30:\"Subscription payment succeeded\";s:10:\"\u0000*\u0000message\";s:125:\"Hi Darwin,\n\nLazaro has been successfully charged for their parking subscription at XYZ Street, XYZ.\n\nwww.xyz.com\n\";s:10:\"\u0000*\u0000trigger\";O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{s:5:\"class\";s:23:\"App\\Models\\Subscription\";s:2:\"id\";i:1259;s:9:\"relations\";a:9:{i:0;s:3:\"bay\";i:1;s:8:\"bay.park\";i:2;s:14:\"bay.park.state\";i:3;s:20:\"bay.park.state.taxes\";i:4;s:13:\"bay.park.user\";i:5;s:5:\"space\";i:6;s:11:\"space.state\";i:7;s:9:\"requestor\";i:8;s:14:\"requestor.user\";}s:10:\"connection\";s:5:\"pgsql\";}s:11:\"\u0000*\u0000category\";s:28:\"subscriptionPaymentSucceeded\";s:13:\"\u0000*\u0000pushStatus\";i:0;s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"}}
@Dallas62 here is the payload
The payload doesn't seems to be a valid Firebase or APN payload. If you are using another provider you should contact this provider.
This is the payload sent via AWS to the device. @Dallas62 we can't yet log anything on the front end as onNotification
isn't firing and the log isn't showing up.
Full version:
{
"default": "Great news, Anthony!\n\nYour booking of No 39 Soi Suanplu, for 1 day was successful.",
"APNS_SANDBOX": "{\"aps\":{\"alert\":{\"title\":\"Booking successful\",\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\"},\"badge\":0,\"sound\":\"default\",\"category\":\"bookingApproved\"},\"object_id\":1567,\"message_category\":\"bookingApproved\"}",
"APNS": "{\"aps\":{\"alert\":{\"title\":\"Booking successful\",\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\"},\"badge\":0,\"sound\":\"default\",\"category\":\"bookingApproved\"},\"object_id\":1567,\"message_category\":\"bookingApproved\"}",
"GCM": "{\"notification\":{\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\",\"title\":\"Booking successful\",\"icon\":\"ic_notification\"},\"data\":{\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\",\"title\":\"Booking successful\",\"object_id\":1567,\"message_category\":\"bookingApproved\",\"badge\":0,\"largeIcon\":\"ic_notification\",\"smallIcon\":\"ic_notification\"},\"priority\":\"high\"}"
}
I'm having the same issue, the only difference is that I'm sending some test notifications using FBC. Relevant code:
export const configure = () => {
PushNotification.configure({
onRegister: function ({token}) {
console.log(token);
},
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
onAction: function (notification) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
},
onRegistrationError: function (err) {
console.error(err.message, err);
},
permissions: {
alert: true,
badge: true,
sound: true,
},
popInitialNotification: true,
requestPermissions: true,
});
};
and then, in index.js
import {configure} from './src/helpers/notifications';
....
configure();
....
AppRegistry.registerComponent(appName, () => App);
The token is printed and but no notifications are displayed. Maybe FB is down?
I double-checked the google-services file which is placed in android/app/google-services
.
Thanks in advance.
There is multiple kind of notifications, do not mix concept. Firebase Console will never trigger onNotification
in background, it's sending notification-only.
To trigger onNotification
in background you must send a data-only notification:
APNS:
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
Configuring a Background Update Notification
Firebase: https://firebase.google.com/docs/cloud-messaging/concept-options
There is already many closed issues about this kind of issue and a TROUBLESHOOTING
file.
There is multiple kind of notifications, do not mix concept. Firebase Console will never trigger
onNotification
in background, it's sending notification-only.To trigger
onNotification
in background you must send a data-only notification: APNS: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html Configuring a Background Update NotificationFirebase: https://firebase.google.com/docs/cloud-messaging/concept-options
There is already many closed issues about this kind of issue and a
TROUBLESHOOTING
file.
@Dallas62 I have added the full version of the payload as well for your reference here https://github.com/zo0r/react-native-push-notification/issues/2017#issuecomment-844057595
This is the payload sent via AWS to the device. @Dallas62 we can't yet log anything on the front end as
onNotification
isn't firing and the log isn't showing up.Full version:
{ "default": "Great news, Anthony!\n\nYour booking of No 39 Soi Suanplu, for 1 day was successful.", "APNS_SANDBOX": "{\"aps\":{\"alert\":{\"title\":\"Booking successful\",\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\"},\"badge\":0,\"sound\":\"default\",\"category\":\"bookingApproved\"},\"object_id\":1567,\"message_category\":\"bookingApproved\"}", "APNS": "{\"aps\":{\"alert\":{\"title\":\"Booking successful\",\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\"},\"badge\":0,\"sound\":\"default\",\"category\":\"bookingApproved\"},\"object_id\":1567,\"message_category\":\"bookingApproved\"}", "GCM": "{\"notification\":{\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\",\"title\":\"Booking successful\",\"icon\":\"ic_notification\"},\"data\":{\"body\":\"Great news, Anthony!Your booking of No 39 Soi Suanplu, for 1 day was successful.\\n\",\"title\":\"Booking successful\",\"object_id\":1567,\"message_category\":\"bookingApproved\",\"badge\":0,\"largeIcon\":\"ic_notification\",\"smallIcon\":\"ic_notification\"},\"priority\":\"high\"}" }
@abhideepmallick Both payload are for notification-only, they will not trigger onNotification
until the notification is pressed. You must send a data-only notification to trigger onNotification
in background. Refer to links provided.
Same issue in Android. Here's my case
onNotification
is executed correctly. (data, title and message arrive OK)onNotification
doesn't get executed (neither if the app is in foreground or background){
"data": {
....
},
"notification": {
"title": "Test title",
"body": "Test body"
}
}
"react-native": "0.63.4", "react-native-push-notification": "7.3.1"
Same issue in Android. Here's my case
- A remote notification arrives
onNotification
is executed correctly. (data, title and message arrive OK)- User presses the notification
onNotification
doesn't get executed (neither if the app is in foreground or background)Notification structure
{ "data": { .... }, "notification": { "title": "Test title", "body": "Test body" } }
Environment
"react-native": "0.63.4", "react-native-push-notification": "7.3.1"
@ignaciosantise It's not the same issue. You can look into the Readme:
If your app has an @Override on onNewIntent in MainActivity.java ensure that function includes a super call on onNewIntent > (if your MainActivity.java does not have an @Override for onNewIntent skip this):
@Override public void onNewIntent(Intent intent) { ... super.onNewIntent(intent); ... }
Or search in issue history for "splashscreen".
Thanks @Dallas62 for your help. It was an issue with the splashscreen. This comment helped me https://github.com/zo0r/react-native-push-notification/issues/1592#issuecomment-691287275
Also had the problem that onNotification was not fired for a remote notification on Android. The Installation section says
NOTE: For Android, you will still have to manually update the AndroidManifest.xml (as below) in order to use Scheduled Notifications.
We were only using remote notification, not scheduled ones, but adding the extra lines to AndroidManifest.xml did the trick for us. Spent lots of time on this issue, but finally started to try the various manual installations options which was how i found this out.
@compelling which lines did you add to make remote notifications for Android work? We are experiencing the same issue with external service notifications
@schlafnie In your android/app/src/main/AndroidManifest.xml make sure this exists:
.....
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application ....>
<!-- 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="false"/>
<!-- 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">
<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>
.....
If are using splash screen then in android you may get the issue of onNotification not getting called:
Solution:
AndroidManifest.xml
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize"
android:exported="true">
MainActivity.java
// Add this method.
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.show(this);
super.onCreate(savedInstanceState);
}
SplashActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
// Pass along FCM messages/notifications etc.
Bundle extras = getIntent().getExtras();
if (extras != null) {
intent.putExtras(extras);
}
startActivity(intent);
finish();
}
Question
Hey I am trying to get the onNotification to run from a remote notification. However I am not being able to get the onNotification to run. Could you suggest me? I have checked the example but it only contains the localNotification codes right? Can you please guide me to a remote notification example?
React native version : 0.61.3 "react-native-push-notification": "^3.5.2",
Here are my codes.
index.js
MainApplication.java
AndroidManifest.xml
build.gradle