Cordova_Ionic_HelloPush

Cordova_Ionic_HelloPush is an example usage of Bluemix push Cordova plugin in Ionic platform.

Prerequisites

  1. Cordova Latest version
  2. Ionic
  3. Android Studio
  4. Xcode

To install the Ionic platform follow this doc

Creating Ionic App.

To create an Ionic app follow the below steps,

  1. Open the terminal app and run the following command to create an app
ionic start {appname} {template}

For Example;

ionic start ExampleApp blank
  1. Go to your appname directory,
cd {appname}

For Example;

cd ExampleApp
  1. Add platforms
ionic platform add ios
ionic platform add android
  1. Add the bms-push plugin to the project
cordova plugin add bms-push

It will add bms-push and bms-core plugins to your app

  1. Edit the www/index.html and add your code for initializing and registering for Bluemix Push notifications. For reference check Example App Index.html & Exaple app push.js

  2. Do the cordova prepare and cordova build.

Note: You may get ios build fail and you can neglect it.

Run Applications

Android

Get your bundle id from config.xml and add it to Firebase app. Download the google-services.json and add to platforms -> android

To run the Android application got to platforms -> android and open it in Android Studio .

Open the build.gradle (Module: android) and,

  • Under buildscript locate dependencies and add classpath 'com.google.gms:google-services:3.0.0'.

  • Scroll down and locate dependencies {compile fileTree(dir: 'libs', include: '*.jar') , ...} and add the following code apply plugin: 'com.google.gms.google-services' just below the ending of dependencies {}

    For example,

  dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    ..........
     .......
     .....
  }
  apply plugin: 'com.google.gms.google-services'

Build and run your application.

iOS

For running the iOS application got to platforms -> ios and open yourApp.xcworkspace in the latest Xcode (8+)

Follow the steps to complete the building of iOS App,

  1. Change the Bundle Identifier and Signing credentials

  2. Go to the AppDelegate.m file and add the following snippets

    Add the #import "yourApp-swift.h"

Objective-C:

// Register device token with Bluemix Push Notification Service
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

  [[CDVBMSPush sharedInstance] didRegisterForRemoteNotifications:deviceToken];
}

// Handle error when failed to register device token with APNs
- (void)application:(UIApplication*)application
didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

 [[CDVBMSPush sharedInstance] didFailToRegisterForRemoteNotifications:error];
}

// Handle receiving a remote notification
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

 [[CDVBMSPush sharedInstance] didReceiveRemoteNotification:userInfo];
}

Swift:

 // Register device token with Bluemix Push Notification Service
 func application(application: UIApplication,
 	didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

 	CDVBMSPush.sharedInstance().didRegisterForRemoteNotificationsWithDeviceToken(deviceToken)
 }

 // Handle error when failed to register device token with APNs
 func application(application: UIApplication,
 	didFailToRegisterForRemoteNotificationsWithError error: NSErrorPointer) {

 	CDVBMSPush.sharedInstance().didReceiveRemoteNotificationWithNotification(error)
 }

 // Handle receiving a remote notification
 func application(application: UIApplication,
 	didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 	fetchCompletionHandler completionHandler: ) {

 	CDVBMSPush.sharedInstance().didReceiveRemoteNotificationWithNotification(userInfo)
 }

 // Handle receiving a remote notification on launch
 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
   let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary

   if remoteNotif != nil {
     CDVBMSPush.sharedInstance().didReceiveRemoteNotificationOnLaunchWithLaunchOptions(launchOptions)
   }
 }

You can follow the this README to setup bms-push.

  1. You have to set the Swift Legacy to yes in your application and in the pod frameworks (BMSPush, BMSAnalytics, BMSAnalyticsAPI, BMSCore and BMSSecurity).

  2. Clean and build the application.

  3. Run the application.

Example App

The ExampleApp is an ionic app using bluemix push notifcations cordova plugin.

  1. Edit the www/js/push.js file with your APPGUID,ClientSecret and App Region. Do the Cordova Prepare and Cordova build.

  2. Add Android and iOS platforms.

 ionic platform add ios
 ionic platform add android
  1. Add the bms-push plugin to the project
cordova plugin add bms-push

It will add bms-push and bms-core plugins to your app

  1. Do cordova prepare and cordova build

  2. To run the iOS app got to platforms -> ios open in Xcode , build and Run the app.

  3. To run the android app got to platforms -> android open in Android Studio , build and Run the app.

Note: In the Android manifest file locate android:launchMode="singleTop" and replace it with android:launchMode="singleTask" to stop restarting the app each time you open the app from notification click.

Copyright 2016-17 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.