In my app I am trying to play a specific sound when new notification comes. It is working fine in android. But in IOS if the app is in background it will work. If the app is killed it will play default notification sound.
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();
if(message.data['navigate_to']=='idea'){
FlutterRingtonePlayer.play(fromAsset: "assets/alerts/trade.mp3",
ios : IosSounds.electronic,
asAlarm: true,);
}
}
I am using flutter_ringtone_player for playing audio.
Also tried with just_audio, assets_audio_player, audioPlayers.
Everything is working when the app is in background only not when killed.
(only in IOS)
Notification sounds work slightly different on iOS than on Android. While on Android, you can play the sound yourself, on iOS this feature is limited by the operating system.
If you want to provide a different sound than the default one, you will have to specify that in the push notification payload, i.e. on the server side. An example on how to specify this sound property in Firebase messaging can be found in this question.
More detailed information about these limitations can be found in Apple's documentation of UNNotificationSound.
The sound file has to be already present on the device
The sound file can be max. 30s seconds long
Related
I'm working on an iOS app that uses MPMusicPlayerController.systemMusicPlayer to play songs from Apple Music to the user.
My app should be able to append songs to the Music player's queue based on messages it receives from a server. I have this working with a simple WebSocket connection between the app and the server, but as soon as the app enters the background the socket is automatically closed (which makes sense).
Because the actual music playback is done by the Music app, I can't use the Background Audio background mode to keep my app alive. Is there a way around this?
Things I have already considered (and why I don't think they will work):
Remote Notifications are throttled too slow to be of any real use
Background App Refresh is also too slow
PushKit / VoIP (the app isn't a VoIP app)
Playing "blank" or nearly silent audio over the actual audio which seems too "hacky" and likely won't pass app review
Using background location tracking (again, almost certainly won't pass review)
Ditching systemMusicPlayer completely and using AVAudioPlayer with the Apple Music API (this would be reinventing the wheel a little bit and would force streaming even if the media was downloaded)
Using applicationQueuePlayer and just forcing the user to stay in app (this would be a bad user experience imo, they should be able to listen in the background)
Any help would be appreciated, thank you!
In iOS 15 and later, you should be able to just set the background plist flag for your app, and then just use ApplicationMusicPlayer.
https://developer.apple.com/documentation/musickit/applicationmusicplayer
I want my iOS Swift app to run in the background. The things I want to run in the background are:
Timer - that pings server periodically and retrieves data from it and then its displayed on the tableView.
Audio - If a certain condition is encountered in the retrieved data, I play a local audio clip from the app via the avf framework.
The app is working as expected. I have done the following to allow backgrounding:
It does work in the background. However, is there any additional thing that I need to do? such that the app doesn't get rejected from the Apple App store.
“Timer That pings the server periodically” - that will make Apple hate you. And if Apple allowed it on the App Store, users whose battery you are emptying will hate you.
Read up on push notifications. No pinging, no energy use at all. Doesn’t even use WiFi or mobile data - your phone service provider sends you the push at a level below mobile data.
I am developing an app in Flutter, which I need to restrict screen recording in iOS devices.
What I have done now is checking for the UIScreen.main.isCaptured flag in the AppDelegate and checking it frequently using a timer in my dart file.
Is there a better way to do this by making use of capturedDidChangeNotification from apple, so that I don't have to run a timer and the dart file get notified when the screen recording status changes?
You add capturedDidChangeNotification observer on NotificationCenter and push the result to FlutterEventChannel. Then on Dart side you setup EventChannel, which has receiveBroadcastStream method that returns a Stream than you can listen to. To be sure to handle closing Streams both on iOS part and Flutter.
You can checkout these official example with listening to battery charging, which is almost the same (NotificationCenter + Streams):
https://github.com/flutter/flutter/blob/master/examples/platform_channel_swift/ios/Runner/AppDelegate.swift
https://github.com/flutter/flutter/blob/master/examples/platform_channel_swift/lib/main.dart
A lot of todo-list apps such as Wunderlist offer some kind of reminder feature which usually just fires off a notification.
But I have never encountered an app that offer a similar notification view as the built in preinstalled alarm clock. You know, that locks the phone and offers a snooze option.
This makes me wonder if the alarm clock functionality that is built into the preinstalled iOs app from Apple including setting time or add a snooze button etc is only available for that particular app?
Since I'm a designer without any coding skills I only want to know if it possible or not.
Unfortunately this is unavailable for developers. You can check all of alarm clock apps in AppStore, all of them are using the standard local notifications. Which will fire once and that's all. You can of course set your custom sound and change couple of options, but it will never work like one build in Clock app inside iPhone.
You can read more about local notifications in Apple documentation: https://developer.apple.com/reference/usernotifications/unnotificationrequest
I figured Alarmy is constantly playing (when there is an alarm set up) a silent sound in the background. Like Spotify, but silent. This keeps the app active all the time. I came to this conclusion by logging the result AVAudioSession.sharedInstance().isOtherAudioPlaying (Swift 4) in my own application. Whenever Alarmy is terminated it returns false, whenever Alarmy is running in the background it returns true. Same thing happens for the app linked by Ernesto Elsäßer.
I used the same technique and I also used this pod to trigger a function when the time comes and start the real music.
I did this for testing and am not going to make an app out of it because playing silent sounds in the background is against Apple's rules. I don't know how Alarmy (or other apps that use this same method) got away with it.
Some related questions:
How do you constantly run in background?
App “Alarmy” is able to play iTunes song from background state… How?
Wake up application in background using AudioSession like Alarmy iOS app
Also you can check this article out.
This app seems to do exactly this, but I have no clue how ...
I tested it, and it managed to start playing music on a locked device at an arbitrarily chosen time, and for way longer than the 30s timeframe allowed for notification sounds. Further, the music is streamed from an online radio station, meaning they found a way to ...
wake up the app from a scheduled notification without user interaction
create a socket connection to a radio station in the background
start playing sound in the background for an indefinite amount of time
I also tried out the alarm in Airplane Mode, and it still played a locally stored backup song, so it probably doesn't abuse the VoiP background mode or push notifications, but really is triggered by a local notification ...
So although I can't tell how, it looks like there is an App Store Review-proof way to create a real alarm app.
(I am not affiliated with the developers of this app, just did some research on what's possible.)
I have been searching everywhere and struggling to find a clear answer. I would like to know if when in background mode, or when the phone is locked, a pebble watch app can communicate with the iPhone to send events.
An example of what I am using it for (in its most basic form) is a stopwatch that runs on your phone and watch. The user can start, stop etc from the pebble watch and the iPhone will send user events to a server...
Any help would be much appreciated.
The Watchface <-> Phone app documentation clearly states that:
Only one iOS application can be connected to the watch at a given time;
Only the iOS application can (re-)open the channel to the watch app.
So your watchface app will be able to send messages to the app, as long as:
Your iOS application is the last 3rd party app that used the channel to the watch
Your iOS application was not killed in the background (for example, you declared a background mode to make sure you would stay online)
Finally, note that the SDK provides you with a callback when the message are not delivered so your watchface app would know if the app was killed and would be able to display a specific message to the user.
This is my understanding of the documentation but I have not been able to try that yet.