Notifications
You can use the Notifications service to send notifications to users of the apps registered in a mobile backend. Once you have the mobile backend set up for notifications, the process of sending the notifications is the same for both iOS and Android apps.
You set up notifications by obtaining the appropriate vendor certificates and registering them with the app's mobile backend. In addition, you include some code in the app itself to enable it to receive notifications. The notifications themselves can be sent via a third-party server or be triggered by logic in a custom API.
Note: Before you start, complete the Get Started with Mobile Development tutorial that is offered within Oracle Mobile Cloud Service on its Home page. That tutorial provides the mobile backend and the quickstart project that you will use here.
Set your application up with your platform vendor
Notifications are sent through the networks provided by your vendor, so you need to do your initial setup with Apple (for iOS apps) and Google (for Android apps).
iOS
To send notifications to iOS apps, you need a provider certificate from Apple (in addition to the certificate used to set up your account).
- Log in to the Apple Developer Center (https://developer.apple.com/) using your Apple Developer account.
- If you haven't yet enabled notifications for your app ID, go back and enable it now.
- Create a Certificate Signing Request (CSR) file for the app ID, install it on your
Mac, and make sure you convert it to a
.p12
file so you can upload it to MCS.
Android
To enable Notifications from the Google Network side, you need to have a project ID, Google Cloud Messaging (GCM) enabled, and an API key.
- Go to the Google Developer's Console (https://console.developers.google.com) and navigate to your project (or create a new project).
- Make a note of the project ID and project name.
- Click the menu icon next to the Google label and select API Manager
- Click the Google Cloud Messaging link for Android.
- Click Enable API.
- In the left navbar, select Credentials.
- Click Add Credentials, select API Key, and then click Server key.
- Type a name for the key and click Create.
- Make a note of the generated key.
Create a notifications profile and register the client app
Next you go back to MCS to handle the following tasks:
- Create a notifications profile to store the vendor credentials.
- Register the app in MCS as a client and associate the notifications profile with the client.
- Associate the client with your mobile backend.
Here are the concrete steps:
- Click and select Applications > Client Management.
- In the left navigation bar, click Client Management.
- Click Profiles.
- In the New Profile dialog:
- Fill in the Name. This can be whatever name that will help you identify the profile most easily.
- Select the Notification Service.
- Fill in the rest of the dialog with the information required by the notification service.
For Apple Push Notification Services (APNS), select Development or Production and then upload your secure certificate.
For Google Cloud Messaging (GCM), fill in the server credentials you obtained from the Google Developer's Console. (You only need to provide the package name if the credentials are scoped to the specific app you are working with in this tutorial.)
- Click Create to create the profile and close the dialog.
- Click Clients.
- Click New Client.
- In the New Client dialog:
- Fill in the Client Display Name and Client Name. (These can be whatever names that will help you identify the client most easily. The former can have spaces and the latter can’t.)
- Select the Platform.
- Fill in the Version Number field.
This version must match the version number of the app as registered with your platform vendor.
- Fill in the fully-qualified app ID. This app ID must match what you specified in the notifications profile.
For Apple, it is the Bundle ID assigned to the application in the Xcode project.
For Google, it is the Package Name for the application as declared in its manifest file.
- Click Create.
- Make a note of the value of the Application Key that is shown in the Settings page. You'll later need to add this key to the app's configuration file.
- From the Mobile Backend dropdown, select a mobile backend to associate with the client.
- Click the Profiles tab and select the notifications profile that you created earlier.
Prepare the client application to receive notifications
You need to configure your app so it can use the device provider’s network for notifications. Here are the steps for iOS and Android.
iOS
As an iOS developer, you need to add some code to get a device token and register your mobile application for notifications.
Code has been added to the sample project to handle these tasks. You just need to uncomment it.
In the appDelegate.m
file, look for the following snippet and delete it:
// Begin commented out Notifications code. /*
Then delete the closing of the comment.
*/ // End commented out Notifications code.
Then, in the LoginViewController.m
file, look for the following line and uncomment it.
//[self registerForPushNotifications];
You also need to update the OMC.plist
file with the client's application key:
- In the
OMC.plist
file, locate theappkey
key and replace the value in its corresponding<string>
element with with the application key that was generated when you registered the app as a client in MCS.
Android
In your client application's main/AndroidManifest.xml
file, within the <application>
tag just
before the closing </application>
, register the
service and any broadcast receivers for the Notifications service,
as shown below. Substitute your app's package name for YOUR.PACKAGE.NAME
.
...
<service android:name="oracle.cloud.mobile.notifications.McsRegistrationIntentService"
android:exported="false" />
<receiver android:name="oracle.cloud.mobile.notifications.McsNotificationsReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="YOUR.PACKAGE.NAME" />
</intent-filter>
</receiver>
</application>
Set permissions to receive and display notifications by
inserting these entries in the Android manifest (somewhere above
the <application> entry). Substitute your app's package
name for YOUR.PACKAGE.NAME
.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <permission android:protectionLevel="signature" android:name=YOUR.PACKAGE.NAME.permission.C2D_MESSAGE" /> <uses-permission android:name="YOUR.PACKAGE.NAME.permission.C2D_MESSAGE" /> <application> ....
Then, in main/java/example/com/gettingstartedandroid/MainActivity.java
,
scroll to the line:
private final String PROJECT_ID = "YOUR_PROJECT_ID";
Replace YOUR_PROJECT_ID
with the project ID that
you obtained from the Google Developer's Console.
Then scroll to the registerNotificationClient()
method to see the code for registering the device for receiving
notifications.
private void registerNotificationClient(){ try { mNotification = MobileBackendManager.getManager().getDefaultMobileBackend(this).getServiceProxy(Notifications.class); mNotification.initialize(this, PROJECT_ID); } catch (ServiceProxyException e) { e.printStackTrace(); } }
Next you need to have your app reference the client's application key:
- In the app's
oracle_mobile_cloud_config.xml
file, insert the following element below the<baseUrl>
element:<appKey></appKey>
- Within that element, insert the application key that was generated when you registered the app as a client in MCS.
Build and deploy the updated application
- In your IDE, run the application.
The IDE should build the application and then launch it in an emulator, simulator, or device of your choosing.
- In the running app, click Sign In, and enter the username and password.
Send the notification
Now let’s go into the Oracle Mobile Cloud Service user interface and send a notification from there.
- Click and select Applications > Mobile Backends.
- Select the your mobile backend and click Open.
- In the left navbar, select Notifications.
- Click the Send icon.
- On the Send Notifications page, type a message and click Send.
- Check the emulator to see if you have received the message.
Next steps
- Learn how to trigger notifications from a custom API.
- Learn everything else about notifications in MCS.
- Browse the MCS SDK reference docs for iOS and Android
- Browse the REST APIs.