ibm-bluemix-mobile-services / bms-pushnotifications-serversdk-java

IBM Cloud Mobile Services - Server side Java SDK for IBM Cloud Push Notifications Service
Apache License 2.0
3 stars 6 forks source link

IBM Cloud Mobile Services - Push Notifications server-side SDK for Java

Build Status Build Status Codacy Badge

The IBM Cloud Push Notifications service provides a unified push service to send real-time notifications to mobile and web applications. The SDK enables Web Apps to receive push notifications sent from the service.

Ensure that you go through IBM Cloud Push Notifications service documentation before you start.

Contents

Installation

You can get the SDK from Maven Central. To get it with Maven, include the following in your dependencies:

<dependency>
    <groupId>com.ibm.mobilefirstplatform.serversdk.java</groupId>
    <artifactId>push</artifactId>
    <version>1.5.0</version>
</dependency>

Initialize SDK

Initialize the SDK with the IBM Cloud region of your application, and optionally, your credentials:

Simple notification

Create Message attributes using builder.

Message message = new Message.Builder().alert("20% Off Offer for you").url("www.ibm.com").build();

Notification options

You can also configure the notification with some other optional settings. Functionality added for FirefoxWeb, ChromeWeb, SafariWeb, ChromeAppExtension and extra optional settings introduced for APNs and FCM are listed in this document.

Builders are introduced which sets the optional settings for the platforms:

  1. Set optional values for all platforms to Settings object.

    Settings settings = new Settings.Builder().apns(apns).fcm(fcm).chromeWeb(chromeWeb)
        .chromeAppExt(chromeAppExt).firefoxWeb(firefoxWeb).safariWeb(safariWeb).build();
  2. Create a new notification.

    Notification notification = new Notification.Builder().message(message).settings(settings).target(target).build(); 
  3. Pass the notification.

    An optional callback ResponseListener is provided if you want to get notified of the result or do some processing with response satusCode or responseBody returned by this callback.

        PushNotifications.send(notification, new PushNotificationsResponseListener(){
            @Override
            public void onSuccess(int statusCode, String responseBody) {
                System.out.println("Successfully sent push notification! Status code: " + statusCode + " Response body: " + responseBody);
            }
            @Override
            public void onFailure(Integer statusCode, String responseBody, Throwable t) {
                System.out.println("Failed sent push notification. Status code: " + statusCode + " Response body: " + responseBody);
                if(t != null){
                    t.printStackTrace();
                }
            }
        });

You will now receive the notification that was sent, on the device that you had registered.

Send bulk Push Notifications

To send bulk push notifications do the following,

    Notification[] ff = new Notification[]{notification1,notification2};
    PushNotifications.sendBulk(ff, new PushNotificationsResponseListener(){
        public void onSuccess(int statusCode, String responseBody) {
            System.out.println("Successfully sent push notification! Status code: " + statusCode + " Response body: " + responseBody);
        }
        public void onFailure(Integer statusCode, String responseBody, Throwable t) {
            System.out.println("Failed sent push notification. Status code: " + statusCode + " Response body: " + responseBody);
            if(t != null){
                t.printStackTrace();
            }
        }
    });

For Javadocs please follow the link - https://www.javadoc.io/doc/com.ibm.mobilefirstplatform.serversdk.java/push

Samples and videos

Learning more

Connect with IBM Cloud

Twitter | YouTube | Blog | Facebook |

======================= Copyright 2020-21 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.