1

I have a xamarin.forms app. I would like to debug push notifications for iOS on an emulator. I have read that in newer versions on xCode I should be able to do that, but for some reason it doesn't work for me. Though the push notifications do come to my app on a real iPhone, it doesn't happen on the emulator. What can I be missing? Again, it does work on a real iPhone. And I just updated the xCode.

David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • Hi, do you use the `xcrun simctl push` to test remote push notification in simulator? – Junior Jiang Dec 16 '20 at 08:52
  • @JuniorJiang-MSFT I use a testing tool at Azure Devops (Notification Hub). – David Shochet Dec 16 '20 at 13:04
  • Hi, I'm not sure whether Azure Devops supports. But you could check whether can get the `PushDeviceToken` In `AppDelegate.cs` first. https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started – Junior Jiang Dec 17 '20 at 02:21
  • @JuniorJiang-MSFT Sorry, it is not clear to me what I should do, how I can check that and why. – David Shochet Dec 18 '20 at 17:48
  • Hi, from the shared document, Step 10 in **[Connect your app to the notification hub](https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started#connect-your-app-to-the-notification-hub)** section, that will return device token. Have a check whether it works in your project. – Junior Jiang Dec 21 '20 at 01:53
  • @JuniorJiang-MSFT RegisteredForRemoteNotifications() is never reached, the breakpoint in it is not hit. – David Shochet Dec 21 '20 at 14:27
  • Okey, therefore the simulator device didn't register to the apple, maybe Azure Devops not supports now. I suggest use [xcrun simctl push](https://www.iosapptemplates.com/blog/swift-programming/push-notifications-simulator-ios) to test whehter it works. – Junior Jiang Dec 22 '20 at 02:45
  • @JuniorJiang-MSFT That is swift... and I work with c# and Xamarin.Forms... – David Shochet Dec 23 '20 at 15:10
  • Hi, I know that's swift. But there is a way to use `xcrun simctl push` to **Trigger Push Notifications**. Because your deice not receiving a device token, that may be the device not be registered to **Apple Push Server**. Therefore, you'd better send the identifier of device or simulator manually. – Junior Jiang Dec 24 '20 at 01:45
  • @JuniorJiang-MSFT My problem is not to sent notifications, but to receive them. – David Shochet Dec 24 '20 at 11:16
  • 1
    Yes, I mean that the problem may be sending method. The general way to send notification, the simulator can not be registed to server. Therefore, we need to send the identifier of device or simulator manually. – Junior Jiang Dec 25 '20 at 01:54
  • @JuniorJiang-MSFT I see, thanks. If you want to make it an answer, I will mark it. – David Shochet Dec 25 '20 at 16:09
  • Okey, I have updated the answer with more detailed information. And there is an another discussion that will be helpful. You could have a look when you have time. – Junior Jiang Dec 28 '20 at 02:07

1 Answers1

1

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

Therefore, I guess the general method to push notification can not register the device for apple push server. You'd better send the identifier of device or simulator by using xcrun simctl push manually.

Command code:

xcrun simctl push <the identifier of device or simulator> <bundle identifier> <path to apns file>

More info can refer to this discussion.

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30