com.ibm.mobilefirstplatform.clientsdk.android.push.api

Class MFPPush

  • java.lang.Object
    • com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush


  • public class MFPPush
    extends java.lang.Object
    MFPPush provides methods required by an android application to be able to receive push notifications.



    Follow the below steps to enable android application for push notifications:



     1. The below permissions have to be set in the AndroidManifest.xml of the android application
    
      <permission android:name="<android application package name>.permission.C2D_MESSAGE"
          android:protectionLevel="signature" />
    
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="<android application package name>.permission.C2D_MESSAGE" />
      <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
      <uses-permission android:name="android.permission.WAKE_LOCK" />
      <uses-permission android:name="android.permission.GET_ACCOUNTS" />
      <uses-permission android:name="android.permission.USE_CREDENTIALS" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    
     2. The activity that is the receiver of push notifications should declare a notification intent in the AndroidManifest.xml as follows:
    
      <intent-filter>
        <action android:name="<android application package name>.IBMPushNotification" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    
     3. Declare the GCM receiver in AndroidManifest.xml to handle messages sent from GCM to application
     
        <receiver
               android:name="com.google.android.gms.gcm.GcmReceiver"
               android:exported="true"
               android:permission="com.google.android.c2dm.permission.SEND" >
               <intent-filter>
                   <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                  <category android:name="<android application package name>" />
               </intent-filter>
        </receiver>
         
     4. Refer to MFPPushIntentService and MFPPushInstanceIDListenerService to declare the intent services in AndroidManifest.xml
    
     5. Sample usage of MFPPush in the android application:
    
      MFPPush push = null;
      MFPPushNotificationListener notificationListener = null;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
    
        // Obtain Push Service
        push = MFPPush.getInstance();
        push.initialize(this);
    
        // Use Push Service APIs
        push.registerDevice(new JSONObject(), new MFPPushResponseListener<String>() {
          @Override
          public void onSuccess(String deviceId) {
            ...
          }
          @Override
          public void onFailure(MFPPushException ex) {
            ...
          }
        });
    
        // Create an object of MFPPushNotificationListener and implement its onReceive method
        notificationListener = new MFPPushNotificationListener() {
          @Override
          public void onReceive(MFPSimplePushNotification message) {
            ...
          }
        };
    
        ...
      }
    
      @Override
      protected void onResume() {
        super.onResume();
        if (push != null) {
          // Request MFPPush to deliver incoming push messages to notificationListener.onReceive() method
          push.listen(notificationListener);
        }
      }
    
      @Override
      protected void onPause() {
        super.onPause();
        if (push != null) {
          // Request MFPPush to stop delivering incoming push messages to notificationListener.onReceive() method.
          // After hold(), MFPPush will store the latest push message in private shared preference
          // and deliver that message during the next listen().
          push.hold();
        }
      }
    
     
    • Method Detail

      • getInstance

        public static MFPPush getInstance()
        Creates the singleton instance of MFPPush
      • initialize

        public void initialize(android.content.Context context,
                      int timeout)
        Initializes the MFPPush instance

        Note: Either this method or MFPPush.initialize(Context) must be invoked after the MFPPush.getInstance().

        Parameters:
        context - Android Context
        timeout - Integer value that specifies time out for the push requests made to the MFP server
      • listen

        public void listen(MFPPushNotificationListener notificationListener)
        Request MFPPush to deliver incoming push messages to listener.onReceive() method.

        This method is typically called from the onResume() method of the activity that is handling push notifications.

        Parameters:
        notificationListener - MFPPushNotificationListener object whose onReceive() method will be called upon receipt of a push message.
      • hold

        public void hold()
        Request MFPPush to stop delivering incoming push messages to notificationListener.onReceive() method. After hold(), MFPPush will store the latest push message in private shared preference and deliver that message during the next MFPPush.listen(MFPPushNotificationListener).

        This method is typically called from the onPause() method of the activity that is handling push notifications.

      • isPushSupported

        public boolean isPushSupported()
        Checks whether push notification is supported.
        Returns:
        true if push is supported, false otherwise.
      • registerDevice

        public void registerDevice(org.json.JSONObject options,
                          MFPPushResponseListener listener)
        Registers the device with the push service
        Parameters:
        options - - Android notification options for e.g JSONObject options = new JSONObject(); options.add("phoneNumber","9999");
        listener - - Mandatory listener class. When the device is successfully registered with Push service the MFPPushResponseListener.onSuccess method is called with the deviceId. MFPPushResponseListener.onFailure method is called otherwise
      • subscribe

        public void subscribe(java.lang.String[] tagNames,
                     MFPPushResponseListener listener)
        Subscribes the device to the given tags
        Parameters:
        tagNames - array of tags
        listener - Mandatory listener class. When the subscription is created successfully the MFPPushResponseListener.onSuccess method is called with the tagNames for which subscription is created. MFPPushResponseListener.onFailure method is called otherwise
      • unsubscribe

        public void unsubscribe(java.lang.String[] tagNames,
                       MFPPushResponseListener listener)
        Unsubscribes the device from the given tags
        Parameters:
        tagNames - array of tags
        listener - Mandatory listener class. When the subscription is deleted successfully the MFPPushResponseListener.onSuccess method is called with the tagNames for which subscription is deleted. MFPPushResponseListener.onFailure method is called otherwise
      • getTags

        public void getTags(MFPPushResponseListener listener)
        Retrieves all the available tags of the application
        Parameters:
        listener - Mandatory listener class. When the list of tags are successfully retrieved the MFPPushResponseListener .onSuccess method is called with the list of tagNames MFPPushResponseListener.onFailure method is called otherwise
      • getSubscriptions

        public void getSubscriptions(MFPPushResponseListener listener)
        Retrieves all the subscriptions of the device
        Parameters:
        listener - Mandatory listener class. When the list of tags subscribed to are successfully retrieved the MFPPushResponseListener .onSuccess method is called with the list of tagNames MFPPushResponseListener.onFailure method is called otherwise
      • setIntent

        public void setIntent(android.content.Intent pushNotificationIntent)
      • openMainActivityOnNotificationClick

        public static void openMainActivityOnNotificationClick(android.content.Context ctx)


© Copyright IBM Corp. 2006, 2015. All Rights Reserved.