onNotificationOpened works in Android emulator but not in actual device - android

I am using firebase notification module in my react-native application. It works perfectly in emulator but when I install the app in mobile, onNotificationOpened event listener never gets called when I open the notification. what am i missing ?
Environment
Development Operating System: Windows
Build Tools:
React Native version: 0.60.5
React Native Firebase Version: 5.5.6
Firebase Module: notifications
Code Snippet:
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
Linking.openURL('https://www.google.com/');
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
Linking.openURL('https://www.google.com/');
}

Related

Notifications not coming when app is closed on Ionic

I have an Ionic project. I can receive notifications when the app is open. But when the app is closed, notifications do not come. After sending 2-3 notifications, the application gives a closed warning. How can I receive notifications when the app is closed?
Ionic info:
Ionic:
Ionic CLI : 6.20.1 (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\#ionic\cli)
Ionic Framework : #ionic/angular 6.5.0
#angular-devkit/build-angular : 13.2.6
#angular-devkit/schematics : 13.2.6
#angular/cli : 13.2.6
#ionic/angular-toolkit : 6.1.0
Cordova:
Cordova CLI : 11.0.0
Cordova Platforms : android 10.1.2
Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0, (and 9 other plugins)
Utility:
cordova-res : 0.15.4
native-run : 1.7.1
System:
NodeJS : v18.13.0 (C:\Program Files\nodejs\node.exe)
npm : 9.3.0
OS : Windows 10
cordova-plugin-fcm-with-dependecy-updated 7.8.0 "Cordova FCM Push
Plugin"
My Code:
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
public route: Router,
public xDevice: Device,
private network: Network,
private fcm:FCM,
) {
this.initializeApp();
this.setupFCM();
}
private async setupFCM() {
this.platform.ready().then(() => {
try {
this.fcm.getInitialPushPayload().then((payload) => {
if(payload?.wasTapped) {
console.log("Received FCM when app is closed -> ", JSON.stringify(payload));
// call your function to handle the data
//this._handlePushNotificationData(payload);
}
});
this.fcm.requestPushPermission();
this.fcm.hasPermission();
this.fcm.getToken().then(token => {
console.log("Token: " + token);
console.log(token)
}).catch(error => {
console.log(error)
})
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
console.log('Received in background');
} else {
console.log('Received in foreground');
}
});
this.fcm.onTokenRefresh().subscribe(token => {
console.log(token)
});
} catch (error) {
console.log(error)
}
});
}
It asks for permission for notification, but when the application is closed, the notification does not come. I can't find the problem either because the app is closed. How can I solve this problem?
The issue you are facing is likely caused by the way Firebase Cloud Messaging (FCM) handles push notifications when the app is closed. FCM uses a notification tray icon to display push notifications when the app is closed. However, the notification may not appear if the user has disabled the notification tray icon for your app.
To troubleshoot this issue, you can try the following:
Ensure that you have correctly set up FCM in your app and that the correct credentials are being used.
Check if the notification tray icon is enabled for your app in the device settings.
Make sure that the FCM server key and sender ID are correctly configured in the Firebase console.
Check if the device token is being correctly retrieved and sent to the FCM server.
5.Test the notifications on different devices and see if the issue is specific to a certain device or if it's happening on all devices.
If the issue is still not resolved, you can try using a different push notification service or library that is better suited for handling push notifications when the app is closed.

Notification Icon not appearing on React-native expo sdk 38 project

I'm having a serious issue using Expo Notifications on SDK 38.0.0. My notification icon does not appear on Android either IOS like this: https://imgur.com/a/6y32HnO. When I remove the flag: "useNextNotificationsApi": true from app.json, my icon appears on Android but, the listener to receive the notification doesn't work anymore. My listener is:
const notificationListener = useRef<Subscription>();
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
// setNotification(notification);
console.log(TAG,'::addNotificationReceivedListener:',notification)
notification.request.content.
});
I'm using react-native with Typescript, and expo SDK 38.0.0, react-native: 0.62.2
expo-notifications:"^0.5.0",

React Native Firebase gives error on device but works on emulator

I'm new to React Native/Firebase. I followed these instructions to setup #react-native-firebase/auth for Android.
I have enabled Email/Password based login from my Firebase web console and have created a user for testing. I am calling Firebase auth using the following code snippet.
auth()
.signInWithEmailAndPassword(userNameText, passwordText)
.then(() => {
navigation.navigate("ClientMainScreen");
})
.catch((error) => {
setSnackBarVisible(true);
});
The thing is that this seems to be running without errors on my Android emulator on my Mac but when testing this out on a physical android device using expo publish, the app crashes when trying to execute this code snippet with the following error log in the Expo Client. What am I missing ?
For context, my index.js looks like this
import { registerRootComponent } from "expo";
import App from "./App";
registerRootComponent(App);
and I'm using react-native": "~0.62.2"

Issue in calling Rest API from Android device

I am trying to call a REST API from Ionic code. The REST API is also developed in my local system. The IP address of my Windows system is say abc.def.gh.i. The GET request API has this URL: http://abc.def.gh.i:8088/check-price/code-001
Problem
I am able call the REST API when I build/run the app using command ionic lab (as this opens up the app on browser). This also works when Ionic code runs on an emulator using the command ionic cordova run android --e78ab88d, as this opens the emulator in same system.
This fails when I run the code on actual Android device by using command ionic cordova run android --device.
Any idea what the issue is?
Here is the provider class that is invoked when an item is passed from the text input:
#Injectable()
export class ServiceProvider {
private urlGetStudentTest: string = "http://abc.def.gh.i:8088/check-price";
callCheckPrice(item) {
console.log("------*****-----" + this.urlcheckprice + '/' + item);
console.log("-----------" + item);
return this.http.get(this.urlcheckprice + '/' + item) .map(res => res.json())
}
}
I can see the two console outputs, after this application stops.

Mobilefirst 8.0 cordova push notification device register failed

I took the sample Cordova project and added platform for Android environment then I created FCM project through Google console and then I got the sender id and server key. I added the MobileFirst server console credentials. Once I did the above steps I added the scope variable in the MobileFirst console "push.mobileclient". Finally I try to run my project using Android studio on an Android emulator.
The testing of the push notification failed while I clicked register device. Below are the error logs:
Failed to register device:"com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushException: Response: Status=400, Text: {\"errorCode\":\"invalid_client\",\"errorMsg\":\"Incorrect JWT format\"}, Error Message: Incorrect JWT format"
Kindly help me to resolve the issue.
Add these plugin in cordova project
cordova plugin add cordova-plugin-mfp
cordova plugin add cordova-plugin-mfp-push
Try to create fresh project in Firebase Console and add Server key & Sender ID in Mobilefirst console carefully.
Run in real device. Also use same network(wifi) in both Mobile and computer.
You can try without scope variable "push.mobileclient" in the MobileFirst console and try sample code:
sample code
function wlCommonInit(){
//initialize app for push notification
MFPPush.initialize (
function(successResponse) {
alert("Push Notification Successfully intialized");
MFPPush.registerNotificationsCallback(notificationReceived);
},
function(failureResponse) {
alert("Failed to initialize");
}
);
//Check device is Supported for push notification
MFPPush.isPushSupported (
function(successResponse) {
alert("Device is Push Supported");
},
function(failureResponse) {
alert("Failed to get push support status");
}
);
//register app for push notification
MFPPush.registerDevice( null,
function(successResponse) {
alert("Device Successfully registered");
},
function(failureResponse) {
alert("Failed to register");
}
);
var notificationReceived = function(message) {
alert(JSON.stringify(message));
};
}
Check here: Not able to send push notification to iOS devices through MFP Server V8 Console

Categories

Resources