I have a NodeJS Server and when my React Native app receive a Push Notification from the Server, the app stop working.
In the Emulator the app just close and in the Cellphone with a Release APK isntalled the app close and show an alert saying that the application stop working.
Here I can show some configuration I added in my Android Application
android/build.gradle
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:4.0.1'
build.gradle
implementation project(':react-native-firebase')
implementation 'com.google.android.gms:play-services-base:15.0.1'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.1.0'
Notifications Listeners
export const configureMessaging = async () => {
try {
const enabled = await FirebaseMessaging.hasPermission();
log('Push Notifications enabled?', enabled);
return enabled;
} catch (err) {
return false;
}
}; // This is returning true
export const configureOnNotificationDisplayed = () => FirebaseNotifications.onNotificationDisplayed((notification) => {
// Process your notification as required
// ANDROID: Remote notifications do not contain the channel ID.
// You will have to specify this manually if you'd like to re-display the notification.
console.log(notification);
});
export const configureOnNotification = () => FirebaseNotifications.onNotification((notification) => {
// Process your notification as required
console.log(notification);
});
export const configureOnNotificationOpened = () => FirebaseNotifications.onNotificationOpened((notificationOpen) => {
// Get the action triggered by the notification being opened
// Get information about the notification that was opened
console.log(notification);
});
I attached a debugger in the AVM and no error was throw
Connected to the target VM, address: 'localhost:8624', transport:
'socket'
Disconnected from the target VM, address: 'localhost:8624',
transport: 'socket'
Note
I do not know what happened in iOS because the app is not configured
to use iOS
The problem was that I had another dependency that was using the Push Notifications. I removed it and everything works perfect
Related
I am using notifee to create notifications in a react native app. I noticed, by default notifications get blocked by android (See under Settings->Apps->My App). Do I have to set the permission somewhere in my app?
When I enable notifications in the Apps-Settings, they work fine, but I'd like them to be enabled when installing the apk.
Yes, You have to explicitly request notifications permission if you targets Android 13.
Paste the following line in AndroidManifest.xml:
<uses-permission android:name="android.permission.POST_NOTIFICATION"/>
And then in your app:
import { PermissionsAndroid } from 'react-native'
const requestNotificationPermission = async () => {
try {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION
)
} catch (err) {
if (_DEV_) console.warn('requestNotificationPermission error: ', err)
}
}
Permission could be named "POST_NOTIFICATION" or "POST_NOTIFICATIONS", depending on your RN version.
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.
My project heavily relies on push notifications or FCM messages for VoIP & messaging features. The issues is these notifications aren't being received by any device when sending a cloud-messaging test message.
Project on Github to view code directly (excluding GoogleServices files): Github Here
Creating a fresh flutter project + firebase project, I performed the following steps:
Create Flutter project, using com.company.pushnotifications + add firebase & messaging deps (ref)
Created a Firebase project
Create Android Firebase App, download GoogleServices.json and place into android/app folder + add required android/build.gradle + android/app/build.gradle plugins (source)
Create iOS Firebase App, download GoogleService-Info.plist, copy into Runner folder using Xcode specifically.
Configure iOS app identity + provisioning profile + APNS key (installed into Firebase Cloud Messaging) with Push notifications + enable Background (fetch + notification) capabilities & push notifications capabilities
do not swizzle notifications by adding the following to the Info.plist file:
Info.plist file:
FirebaseAppDelegateProxyEnabled -> NO (Boolean)
edit ios/Runner/AppDelegate.swift with the following content:
AppDelegate.swift content:
import UIKit
import Flutter
import Firebase // added this line
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure() //add this
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Setup firebase messaging with the following flutter code:
I'm debugging with breakpoints in background & foreground messages, left it for 2 hours incase the messages were delayed but on not one of the debugging sessions did a breakpoint trigger indicating a message was received
main.dart file:
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp(); // breakpoint for debugging placed here
print("Handling a background message: ${message.messageId}");
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseApp defaultApp = await Firebase.initializeApp();
// source: https://firebase.flutter.dev/docs/messaging/usage#background-messages
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
// source: https://firebase.flutter.dev/docs/messaging/usage#foreground-messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!'); // breakpoint for debugging placed here
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
runApp(MyApp());
}
When sending a test push notification using Cloud Messaging Test notifications, I expect the notification to show on Android (emulated or physical atleast), but it does not show on either iOS or Android (emulated or physical).
To my knowledge I've followed the documentation as required - did I miss something or is something broken?
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/');
}
I want to send sms using ionic framework.I install this plugin:
cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git
In javascript:
document.addEventListener("deviceready", function() {
var options = {
replaceLineBreaks: false, // true to replace \n by a new line, false by default
android: {
intent: '' // send SMS with the native android SMS messaging
//intent: '' // send SMS without open any other app
//intent: 'INTENT' // send SMS inside a default SMS app
}
};
$cordovaSms
.send('+919915768727', 'This is some dummy text', options)
.then(function() {
alert('Success');
// Success! SMS was sent
}, function(error) {
alert('Error');
// An error occurred
});
});
Message is not send.this will alert error.I dont know where I am wrong.
cordova version: 5.2.0
Ionic version: 1.6.4
you have place the function of $cordovaSMS in an $ionicPlatform function as
$ionicPlatform.ready(function(){
$cordovaSms
.send($scope.form.number, $scope.form.message, options)
.then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
})
})
and I am uploading a demo project for you in git hub follow the steps in readme.md file of github
Having any queries replay back