[SalesForce] Mobile Push Notifications: to which device/connected app should I send

I'm developing a mobile app for both Android and iOS that will receive push notifications. I've followed the instructions in the Salesforce Mobile Push Notifications Implementation Guide and created a Connected App for each platform. I've configured push for each app, and I can successfully push using the 'Send Test Notification' link on each connected app's management page.

The documentation lists instructions for sending push notifications from Apex using Messaging.PushNotification; the process is slightly different for iOS and Android, and you pass the connected app's name when you send the notification.

So, if I want to send a push notification to Joe User, how do I know which type of notification to send? The sample code only shows pushing an iOS notification. He could have iOS or Android. When the mobile app registers, it registers for a specific connected app; the platform knows which device(s) the user has. When you send a test push notification, you can see the most recent push registrations for that connected app and even search by name – but that object (which I suspect is named Mobile Push Service Device) does not appear to be queryable.

I suppose I could have each mobile app register something on the user record or similar, but I feel like I'd be duplicating the push registration functionality, only without all the smart bits that track when a push registration is no longer valid. Should I just try to send both types of notification to every User? Seems like a waste of resources.

Best Answer

When you send push notifications from Apex, you don't need to know which devices users have registered. You need to create a list of user ids for the users you want to send notification to. Then create Messaging.PushNotification object, set a payload for this object and use its "send" method to send push notifications to the list of users. Here is an example:

https://developer.salesforce.com/docs/atlas.en-us.pushImplGuide.meta/pushImplGuide/pns_apex_trigger.htm

If you have 2 applications that you want to send the same notifications to (1 for Android and 1 for iOS), you will call "send" method 2 times for the same list of user ids but with different application name. You can also create different Messaging.PushNotification objects for iOS and Android notifications and use different payload if you need.

Related Topic