Flutter firebase send test message to iOS emulator [duplicate] - flutter

Is it possible test the Apple Push Notification Services without an iPhone application? (Creating an emulator on windows?)
If isn't, how could I test that? Is there a free sample application compiled to do that?
I created the Server provider, but I need test the functionallity.

This answer is outdated. As of 2020 / Xcode 11.4 it's now possible to test push notifications in the simulator
See this full explanation in an answer below
Sorry to say, but you'll need to find some hardware to test this functionality.
Push notifications are not available in the simulator. They require a provisioning profile from iTunes Connect, and thus are required to be installed on a device. That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $99.
On the bright side, with the iPhone OS 3.0 update, you can test this functionality on any device, including the first gen iPhones.

You can't test real push notifications. However, you can test your app's response to a simulated push notification by creating one programmatically and manually triggering your AppDelegate's - application:application didReceiveRemoteNotification:notification method.
To trigger this from a different class (like a UIViewController):
[[[UIApplication sharedApplication] delegate]
application:[UIApplication sharedApplication]
didReceiveRemoteNotification:testNotification];
The testNotification should have the same format as a real notification, namely an NSDictionary that contains property list objects plus NSNull.
Here's an example of how to provide the testNotification above:
NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
[notification setValue:#"Test" forKey:#"alert"];
[notification setValue:#"default" forKey:#"sound"];
NSMutableDictionary *testNotification = [[NSMutableDictionary alloc] init];
[testNotification setValue:notification forKey:#"aps"];
This should create a reasonable notification NSDictionary to work with.

Testing push notifications using the Xcode 11.4 iOS Simulator
As of Xcode 11.4, it is now possible to simulate push notifications by dragging and dropping an .apns file onto the iOS simulator. The Xcode 11.4 release notes have the following to say about the new feature:
Simulator supports simulating remote push notifications, including
background content fetch notifications. In Simulator, drag and drop an
APNs file onto the target simulator. The file must be a JSON file with
a valid Apple Push Notification Service payload, including the “aps”
key. It must also contain a top-level “Simulator Target Bundle” with a
string value matching the target application‘s bundle identifier.
simctl also supports sending simulated push notifications. If the file
contains “Simulator Target Bundle” the bundle identifier is not
required, otherwise you must provide it as an argument (8164566):
xcrun simctl push <device> com.example.my-app ExamplePush.apns
Example
Here is an example for such an .apns file, targeted towards the system settings app:
{
"Simulator Target Bundle": "com.apple.Preferences",
"aps": {
"alert": "This is a simulated notification!",
"badge": 3,
"sound": "default"
}
}
Dragging and dropping this onto the target simulator will present the notification and set the badge:
Now, to use this for debugging purposes, you only have to change the Simulator Target Bundle to your own app's identifier and adjust the payload to your debugging needs!

Nowadays, we can test push notifications with this library.
It's pretty easy to send push via terminal:
echo -n '{"message":"message"}' | nc -4u -w1 localhost 9930
echo -n '{"aps":{"alert" : "message","badge" : 99,"sound" : "default"}, "myField" : 54758}' | nc -4u -w1 localhost 9930

Apple has supporting push notification for simulators.
iOS 13.4 and above or Xcode 11.4 and above.
as usually create Xcode project and implement a user notification and authorization permission.
run your application in simulator iOS 13.4 and above.
put your application in background.
Create an APNS payload file named "payload.apns"
{
"aps": {
"alert": {
"title": "Test Push",
"body": "Success! Push notification in simulator! 🎉",
"sound": "default"
},
"badge": 10
},
"Simulator Target Bundle": "com.company.app"
}
Drag and drop to your iOS simulator.
right now your push notification will appear on simulator.
And also you can simulate push notification by terminal
get your simulator identifier by opening Window->Devices and Simulators and choose your targeted simulator and right click and copy your identifier.
Now build a terminal command like
xcrun simctl push <simulator-identifier> <path-to-payload-file>
ex:
xcrun simctl push 27A23727-45A9-4C12-BE29-8C0E6D1E5360 payload.apns
run this command and simulate push notification in simulator

The simulator does not do Push Notifications.
And to push from a server, you have to have device(s) to push to as well as your app on that device.
The token contains the app identity as well as the device ID.

Xcode 11.4 Beta starts supporting push notification in Simulator
Simulator supports simulating remote push notifications, including background content fetch notifications. In Simulator, drag and drop an APNs file onto the target simulator. The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key. It must also contain a top-level “Simulator Target Bundle” with a string value matching the target application‘s bundle identifier.
simctl also supports sending simulated push notifications. If the file contains “Simulator Target Bundle” the bundle identifier is not required, otherwise you must provide it as an argument (8164566):
$ xcrun simctl push <device> com.example.my-app ExamplePush.apns
Release Notes: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes

With macOS 13 and Xcode 14, when on Mac computers with Apple silicon or T2 processors, it’s now possible to receive real push notifications as didRegisterForRemoteNotificationsWithDeviceToken will return a real token.
See Xcode 14 release notes:
Simulator now supports remote notifications in iOS 16 when running in macOS 13 on Mac computers with Apple silicon or T2 processors. Simulator supports the Apple Push Notification Service Sandbox environment. Your server can send a remote notification to your app running in that simulator by connecting to the APNS Sandbox (api.sandbox.push.apple.com). Each simulator generates registration tokens unique to the combination of that simulator and the Mac hardware it’s running on.
https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes

you have to use
NSString *notificationString = #"{\"aps\":{\"alert\":\"Test alert\",\"sound\":\"default\"}}";
NSData *notificationData = [notificationString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *testNotification = [NSJSONSerialization JSONObjectWithData:notificationData options:0 error:&error];
[[[UIApplication sharedApplication] delegate] application:[UIApplication sharedApplication] didReceiveRemoteNotification:testNotification fetchCompletionHandler:nil];

Apart from #fredpi's answer, you can also use the Poes command-line tool. It allows us to quickly test remote push notifications on the iOS simulator without the hassle of creating a JSON payload file.
Poes --help
OVERVIEW: A Swift command-line tool to easily send push notifications to the iOS simulator
USAGE: Poes <options>
OPTIONS:
--body, -b The body of the Push Notification
--bundle-identifier The bundle identifier to push to
--mutable, -m Adds the mutable-content key to the payload
--title, -t The title of the Push Notification
--verbose Show extra logging for debugging purposes
--help Display available options
The following command could be enough to send out a simple push notification with a default title and body:
$ Poes --bundle-identifier com.wetransfer.app --verbose
Generated payload:
{
"aps" : {
"alert" : {
"title" : "Default title",
"body" : "Default body"
},
"mutable-content" : false
}
}
Sending push notification...
Push notification sent successfully

Yes, you can check push notification on the simulator, but you have to use a library in your app named SimulatorRemoteNotifications. By which, by using just 4-5 steps, you can test push notification on the simulator.
They also provide PODs too:
pod 'SimulatorRemoteNotifications', '~> 0.0.3'

It looks like now we have very powerful lib https://github.com/ctreffs/SwiftSimctl
At least it's much more power than lib SimulatorRemoteNotifications which were mentioned here. And which was obsoleted also.

For a more complete answer since Xcode 11.4 and as of Xcode 12.4:
Simulator supports simulating remote push notifications, including background content fetch notifications. In Simulator, drag and drop an APNs file onto the target simulator. The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key. It must also contain a top-level “Simulator Target Bundle” with a string value that matches the target application’s bundle identifier. simctl also supports sending simulated push notifications.
Notification Service Extensions do not work in simulated push notifications. The mutable-content key is not honored. (55822721)
Reference: Xcode 11.4 Release Notes

Most answers suggest using a third-party Cocoapods/SPM dependency. But these dependencies can easily become unsupported for quite a while after a new iOS or Xcode release.
Here is just a lightweight solution that will work in any case and in any weather:
Start any HTTP server on localhost before running the tests (e.g.: ruby/python)
Send a POST request to this localhost from the test when you need to trigger a push notification (e.g. via URLSession)
Get a request on localhost and execute an xcrun simctl push command
For more details feel free to check out this blog post. Hope this helps.

Related

Xcode 8 ios 10 Simulator , is it possible to send push notification to the simulator

My app uses Firebase. I am trying to send push notification to app running in simulator. When I run the app in iOS 10 Simulator, it successfully generates InstanceID token but shows warning that
notifications are not supported in the simulator.
I read somewhere on stackoverflow that it is possible to send push notification on iOS 10 simulator. Is it true?
You cannot get push notifications inside the simulator. You can, however simulate getting a push notification by making a button or whatever that calls application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) with a notification dictionary. This is useful for testing that the rest of your logic is working before you deploy to device to test that the final format of the push is what you expect it to be.
Two things:
You cannot test push notification in simulator. So you have to test only in iPhone device only. Additional information: If you wan to test the push notification locally in your device try using Test push notification
You said you heard somewhere that " it is possible to send push notification on iOS 10 simulator". It is not true, The thing is in iOS 10 you can view the local notification even when the app is in foreground. using the following delegate method "willPresentNotification" supported from iOS 10. Which you can test in simulator.
Now It Is Possible!!!
The Xcode 11.4 beta is released with simulator supporting remote push notifications.
To test remote push simply dragged APNS file onto the target simulator. This method required the payload to contain Simulator Target Bundle key. Otherwise, you would get this error message.
“Invalid push notification: The file does not contain a valid JSON payload or the Simulator Target Bundle key is missing."
So your payload file will be like:
{
"aps" : {
"alert" : {
"title" : “Namaste”,
"body" : “This is iOS development notification test“
},
},
"Simulator Target Bundle": "com.sarunw.example-xcode-11-4"
}
Following simctl command in terminal will help to sent notifications:
xcrun simctl push <device> <bundle-identifier> <path-to-apns-file>
From onward Xcode 11.4 Beta you can test push notifications on iOS simulator.
The Xcode 11.4 Beta release notes
The simulator supports simulating remote push notifications, including background content fetch notifications.
In Simulator, drag and drop an APNs file onto the target simulator. The file must be a JSON file with a valid Apple Push Notification
Service payload, including the “aps” key. It must also contain a top-level “Simulator Target Bundle” with a string value matching the target application‘s bundle identifier.
Now it is possible to receive a Push notification (with sound and badge count) in Simulator
click here
https://stackoverflow.com/a/60282092/2302313

Push Notifications in Mavericks iOS Simulator

I can't find anything about this, and I have never done anything with push notifications (but I know vaguely how they work). In Mavericks, now that OS X can receive push notifications from various things like websites, can iPhone Simulator receive push notifications? I've found plenty of answers from mid-2013 and earlier saying "no", but all of them are outdated because Mavericks was just recently released with its new push features.
I'm about to start working on an app for a school project that should use push notifications unless I want to be cheap and just poll the server. I understand that you NEED an iOS developer account to use push notifications, but it would make my life a lot easier if I could test on my computer.
iOS 8 and Xcode 6 did indeed add some additional integration to push notifications. If you select the iPhone 6 or iPhone 6 Plus simulator your app will now appear under Settings with a full featured notifications settings screen (see attached) and will show the in app modal when requesting push notification permissions for the first time. This is only true for the iPhone 6 and iPhone 6 Plus simulators.
However, in iOS 8 you will still get message in console as 'remote notifications are not supported in the simulator' and hence you have to use device only.
No, there is still no API to support push notifications in the simulator. You’ll have to use a device.
Even though iPhone 6 and iPhone 6+ simulators have setting for push notifications and even show you the push notification prompt, an attempt to register for push notifications from simulator still fails with Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x7fc786b4af90 {NSLocalizedDescription=remote notifications are not supported in the simulator}. So, your best bet is still to use a device.
Yeahhh!! With the release of XCode 11.4, Now it is possible to receive Push notification in Simulator too.
Apple's latest release says,
Simulator supports simulating remote push notifications, including
background content fetch notifications.
In Simulator, drag and drop an APNs file onto the target simulator.
The file must be a JSON file with a valid Apple Push Notification
Service payload, including the “aps” key.
It must also contain a top-level “Simulator Target Bundle” with a
string value matching the target application‘s bundle identifier.
Reference Link: https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_beta_release_notes
Example payload file,
{
"Simulator Target Bundle": "com.yourOrganization.appName",
"aps": {
"alert": "This is a test notification!",
"badge": 5,
"sound": "default"
}
}
#note: mention your application's Bundle Identifier in "Simulator Target Bundle" in the above Payload.
Dragging and dropping above json file onto the target simulator will present the notification and set the badge.
Update XCode8 GM Release
Log: Push notifications now fail with the old haunting poem:
remote.
notifications are not.
supported in the simulator.
I now question myself and my sanity. This feature that I'd long hoped for, and for a brief moment had in my hand, or so I thought. Was it all just a dream? Are any of us really here?
<fade out...>
Original Post
As of the iOS10 beta (XCode Version 8.0 beta (8S128d)), simulators seem to be getting Push notifications. Note that although prior to iOS10, devices could receive local notifications, they did not receive push notifications.
I don't have any official source on this, just anecdotally, features that rely on push notifications which used to fail, are now succeeding on iPhone 5, 5s, 6, 6+ simulators.
I also verified that we're hitting the registration block in AppDelegate.
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(#"Successfully registered!");
doSomething(notificationSettings);
}
Looking forward to using remote notifications in the simulator when iOS 10 comes out.
In the meantime I use the Xcode command line to test notification behavior:
p [((CustomAppDelegateClass *)((UIApplication *)[UIApplication sharedApplication]).delegate) application:[UIApplication sharedApplication] didReceiveRemoteNotification:#{#"aps": #{#"alert": #"Alert Message"}, #"info": #{#"object": #"value"}}];
Now it is possible to receive a Push notification (with sound and badge count) in Simulator
Step 1 :- Create Project (Xcode 11.4 beta)
Step 2 :- import UserNotifications and registerForPushNotifications
Step 3 :- Create JSON file (payload.apns) like below
{
"Simulator Target Bundle": "metiz.DemoSimPushnotification",
"aps": {
"title": "Xcode 11.4 Beta Update",
"alert": "Push notification in simulator - take a look",
"sound": "default",
"badge": 1
}
}
Step 4 :- Open terminal write command $ xcrun simctl push
com.example.myapp ExamplePush.apns”
< device > will be replaced by the device identifier.
com.example.myapp  will be replaced by your project’s bundle identifier
ExamplePush.apns will be replaced by the filename of our apns file.
like this:-
$ xcrun simctl push 4F19A097-DAE2-4298-99EB-23D4F2DBAF97 metiz.DemoSimPushnotification payload.apns
Now, press Enter! see the magic!
Reference link:-
https://www.metizsoft.com/blog/testing-push-notification-on-ios-simulator

Using iCloud in iOS Simulator

In my App, when I try to run code in the iOS Simulator:
NSURL *iCloudURL = [fileManager URLForUbiquityContainerIdentifier:#"2VHM28566N.com.eept.TestICloud"];
NSLog(#"IS ICloud : %#", [iCloudURL absoluteString]);
It shows that iCloudURL is nil.
Can we use iCloud without an iPhone device?
I tried to run my iCloud app today on the simulator and there is no option to enable iCloud in the simulator as far as I can see. I looked in settings, but no luck. So the answer would be no, you can't really test iCloud unless you test it on an actual device.
If anyone has a workaround, that would be fabulous. It's a bit tedious to always test your app on the device.
In Xcode 5:
iOS Simulator now supports iCloud syncing of documents and KVS data
within an app, enabling apps to sync between devices using iCloud.
This feature is useful when testing to ensure that the app documents
and data are syncing properly across multiple devices.
Note: With the app running in the iOS Simulator, sign in to an Apple
ID account using the Settings app. After signing in, use the “Trigger
iCloud sync” command in the Debug menu to tell the simulator to sync
with other devices.
I know this is an old topic, but you can test iCloud by changing your deployment target to 'Device'. Plug your device in via USB and let the fun begin.
I would imagine that this is done for security reasons, and for a very good reason at that.
Because your device will have a mobile provisioning certificate which ties up with your iCloud 'bucket' as well as your App Bundle ID, it ensures that only you (your app and allowed devices) have access to your provisioned iCloud bucket.
If you could run it on the simulator without having all of these certificates and ID's in place, you could easily get a team ID from any other app which you download, slap together a project using that identifier and without a certificate marrying up developer, with iCloud bucket, you'd get full access to another App's bucket.
All I did to get around this was order a 5m USB extension so I can have my iPads, iPods and iPhones on the desk in front of me without having to crouch under a desk or sit in awkward positions whilst testing.
edit Just to add an slightly clearer answer as to what allthewayapps asks about the bundle ID.
2VHM28566N.com.eept.TestICloud
is made up of 3 parts in this case:
2VHM28566N - Being the TeamID which Apple assign you when you register as a developer
com.eept - Reverse domain notation of the App's related website i.e. Apple apps would have com.apple
TestICloud - The name of that app itself.
In short its:
teamid.com.yourdomain.appname
Hope this helps.

Push notification flag in Settings

When a app has a push notification feature implemented at what point in time do we see the app name under Settings-->Notifications for enabling/disabling notifications?
Is it when you install the app and IOS will automatically come to know this or we need to run the app at-least once to see this option.
I am seeing two different cases: When I install the app on my iPod touch I immediately see the Push notification enabling switch in Settings but when I install it on my iPhone it do not show this option even after running the app. What could be the reason?
It shows when you register the app for push notifications in code. The reason it is showing up for you as soon as you install it on one of your devices is that you have already accepted it on a previous install of the same app, and uninstalling/reinstalling unfortunately does not cause iOS to forget this.

Push Notification in iPad not appear in Setting

I am working on push notification on iPad. My application is a universal application include iPhone and iPad. The iPhone push notification works well, the user receives push, the app name appears in the Setting -> Notifications
However, I have a small issue with the ipad version is that the app name does not appear in the list of Settings -> Notifications. I can still register for the device ID, the iPad does receive the push notification.
Does anyone know what can be a reason? I checked and be sure that both the registering code for both devices are exactly the same
Maybe this is obvious, but did you try to:
Restart the iPad?
Completely remove the App, create a clean build and run that build on the iPad?
Try to install on another iPad? Is the issue also present on the other iPad?
Reinstalling a clean build and restarting the device should do the trick with the majority of strange problems like this one. If this is not resolving your problem it's very important to test on another iPad or completely reset your iPad.
This way you can see if there is something wrong in the iOS install itself. It sounds like this is the case, because push is working and the iPhone version is working fine.
By testing on another iPad you could determine if the problem is in your code/App or in iOS.
Is your iPad updated to the latest version?
Follow the steps mentioned below.
http://ipad.about.com/od/iPad_Guide/ss/How-To-Turn-Off-Push-Notifications-On-The-Ipad.htm
Are you sure you are registering your app at launch time?
I had a similar issue where I removed - (void)registerForRemoteNotificationTypes by accident.
All the devices on which I had the application before removing that line still had the settings, but the new one didn't register to push hence not being registered in Settings.