I develop flutter project and I use FCM and Local Notification I have issues when iOS phone in background it's not vibrate.
my BackgroundHandler:
#pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
await setupFlutterNotifications();
showFlutterNotification(message);
}
I use
await Vibration.vibrate(duration: 500);
to vibration.your text
In my opine you can't because notification vibrate is depends on phone settings so you can't do that even forcefully if notification vibration disable from phone settings.
for more details check this link it may helpful.
Related
I am using flutter to develop my app , firebase auth for user authentication and firebase messaging for data notifications when app is in background and terminated state.
In IOS,
when app does a fresh firebase authentication and generate a fcm token, notifications are received when app is in background state, but once i kill the app from background , and launch it again , the notifications stop being received by the app until i do a firebase auth verification and generate a new fcm token again.
in main.dart file in the main function initializeFirebase function is
called .
Future initializeFirebase() async {
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(
_onAppInTerminatedNotificationHandler, // This Callback must be static
);
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
}
Future<void> _onAppInTerminatedNotificationHandler(
RemoteMessage message,
) async {
//await Firebase.initializeApp();
print('A new terminated Push Notification Received');
await NotificationManager.instance.showLocalNotification(message);
}
in iOS, there will be a pop up that will ask the user to accept or deny push notification permission.
if the user deny the permission or they set to not to display the push notification in the settings, will the FCM listeners to receive FCM message still be called? I mean this lines below.
in Android, if I disable to display the push notification in phone settings, then the listeners below will still be called. but unfortunately I can't test it for iOS since I don't have apple developer account yet at the moment
// to receive foreground message
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
});
// to receive background message
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print("Handling a background message: ${message.messageId}");
}
From Cloud Firestore | Requesting permission (Apple & Web),
On iOS, macOS & web, before FCM payloads can be received on your
device, you must first ask the users permission. Android applications
are not required to request permission.
So you need permission for the listeners to be called on iOS.
I am using Google Firebase to send push notifications. For Android client, I only send data (not notification) and if notification required, I send it as local notification and it works fine. Data arrive whether the app is running or killed or in the background. In IOS, I configured APNs with Firebase but I have some trouble about background data and notification. The code below sends background data to IOS client by using APNs. "content_available" flag direct notification to APNs but as Firebase documentation mentioned, APNs are not guaranteed to be delivered. The link below explains it.
https://firebase.google.com/docs/cloud-messaging/http-server-ref
"On iOS, use this field to represent content-available in the APNs payload. When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server. Note that silent notifications in APNs are not guaranteed to be delivered, and can depend on factors such as the user turning on Low Power Mode, force quitting the app, etc. On Android, data messages wake the app by default. On Chrome, currently not supported."
If I add the codes at the comment lines, APNs sent always notification whether the app is in foreground, background or killed. Of course, I don't want push notification if the app is in the foreground. Furthermore, if the user doesn't click the notification (click the app icon), the code is not triggered. Is there any way to use APNs as Firebase on Android? In Android background data notification always sent to the client successfully whether the app is in the foreground, background or killed.
private static String apiKey = "AIzaSy............";
public static void sendNotification(JSONObject jsonData, String token) {
try {
JSONObject jsonGCM = new JSONObject();
jsonGCM.put("to", token);
jsonGCM.put("data", jsonData);
jsonGCM.put("content_available", true);
//jsonGCM.put("priority", "high");
/*JSONObject jsonNotification = new JSONObject();
jsonNotification.put("title", "Some Title");
jsonNotification.put("body", "Some body.");
jsonGCM.put("notification", jsonNotification);*/
URL url = new URL(
//"https://gcm-http.googleapis.com/gcm/send");
"https://fcm.googleapis.com/fcm/send");
HttpsURLConnection conn = (HttpsURLConnection) url
.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type",
"application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key="
+ apiKey);
OutputStream os = conn.getOutputStream();
os.write(jsonGCM.toString().getBytes("UTF-8"));
os.flush();
InputStream in = new BufferedInputStream(
conn.getInputStream());
System.out.println("Response code -->"
+ conn.getResponseCode());
os.close();
}
catch (Exception e) {
// TODO: handle exception
}
}
I have been using firebase unity(beta) plugin for push notification. I can get push notification data when my app is opened from notification screen with this method.
public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
Debug.Log("Received a new message");
}
But I can't get push notification data when my app is in foreground.
How can I get this data?
Anybody help?
This solution solved my problem.
public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
NotificationServices.GetRemoteNotification (NotificationServices.remoteNotificationCount-1);
}
Old thread but if anyone else comes across this, I was receiving messages all of the time on Android but on iOS, OnMessageReceived would not fire until I added content_available: true to the firebase notification payload.
Now, all is good and I can trigger actions in my foreground app through arbitrary data.
I am creating a hybrid app in ionic and want to show a notification when the app is in foreground. Based on research, the best way to do that is via cordova local notifications but though it works perfectly in Android and shows a banner with sound.
On ios, it only puts the notification in the notification try and doesnt make any sound. Can anyone help me out with this?
Here is my code
cordova.plugins.notification.local.registerPermission(function (granted) {
console.log('Permission has been granted: ' + granted);
cordova.plugins.notification.local.schedule({
text: data._raw.message,
at: alarmTime,
data: data._raw.additionalData.loan_id
});
});
You have to listen for the event when the notification is received. From your code you only schedule a notification to be sent but you do not handle receiving the notification.
This is how you register the event when the notification triggers:
$rootScope.$on('$cordovaLocalNotification:trigger',
function (event, notification, state) {
// Add some logic here:
console.log("received: ", notification);
});
Check the ngCordova docs for more events and information.