public class MFPPush
extends java.lang.Object
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 toMFPPushIntentService
andMFPPushInstanceIDListenerService
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(); } }
Modifier and Type | Method and Description |
---|---|
static MFPPush |
getInstance()
Creates the singleton instance of MFPPush
|
void |
getSubscriptions(MFPPushResponseListener listener)
Retrieves all the subscriptions of the device
|
void |
getTags(MFPPushResponseListener listener)
Retrieves all the available tags of the application
|
void |
hold()
Request MFPPush to stop delivering incoming push messages to
notificationListener.onReceive() method.
|
void |
initialize(android.content.Context context)
Initializes the MFPPush instance
|
void |
initialize(android.content.Context context,
int timeout)
Initializes the MFPPush instance
|
boolean |
isPushSupported()
Checks whether push notification is supported.
|
void |
listen(MFPPushNotificationListener notificationListener)
Request MFPPush to deliver incoming push messages to listener.onReceive()
method.
|
static void |
openMainActivityOnNotificationClick(android.content.Context ctx) |
void |
registerDevice(org.json.JSONObject options,
MFPPushResponseListener listener)
Registers the device with the push service
|
void |
setIntent(android.content.Intent pushNotificationIntent) |
void |
subscribe(java.lang.String[] tagNames,
MFPPushResponseListener listener)
Subscribes the device to the given tags
|
void |
unregisterDevice(MFPPushResponseListener listener)
Unregister the device from the push service
|
void |
unsubscribe(java.lang.String[] tagNames,
MFPPushResponseListener listener)
Unsubscribes the device from the given tags
|
public static MFPPush getInstance()
public void initialize(android.content.Context context)
Note: Either this method or MFPPush.initialize(Context, int)
must be invoked after the MFPPush.getInstance()
.
context
- Android Context
public void initialize(android.content.Context context, int timeout)
Note: Either this method or MFPPush.initialize(Context)
must be invoked after the MFPPush.getInstance()
.
context
- Android Context
timeout
- Integer value that specifies time out for the push requests made to the MFP serverpublic void listen(MFPPushNotificationListener notificationListener)
notificationListener
- MFPPushNotificationListener object whose onReceive() method
will be called upon receipt of a push message.public void hold()
MFPPush.listen(MFPPushNotificationListener)
.
This method is typically called from the onPause() method of the activity
that is handling push notifications.public boolean isPushSupported()
public void registerDevice(org.json.JSONObject options, MFPPushResponseListener listener)
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 otherwisepublic void subscribe(java.lang.String[] tagNames, MFPPushResponseListener listener)
tagNames
- array of tagslistener
- 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 otherwisepublic void unsubscribe(java.lang.String[] tagNames, MFPPushResponseListener listener)
tagNames
- array of tagslistener
- 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 otherwisepublic void unregisterDevice(MFPPushResponseListener listener)
listener
- Mandatory listener class. When the device is deleted
successfully the MFPPushResponseListener
.onSuccess
method is called. MFPPushResponseListener
.onFailure method is
called otherwisepublic void getTags(MFPPushResponseListener listener)
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
otherwisepublic void getSubscriptions(MFPPushResponseListener listener)
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
otherwisepublic void setIntent(android.content.Intent pushNotificationIntent)
public static void openMainActivityOnNotificationClick(android.content.Context ctx)
© Copyright IBM Corp. 2006, 2015. All Rights Reserved.