Search code examples
ibm-mobilefirstworklight-server

Mobile first server 7.1 - Unsubscribe from Push Notification (Unicast Notification)


We are making an hybrid application Using Mobile First Platform. For push notification we will be using Unicast notifications. I could not find any documentation regarding unsubscription. Can any one help me to know how can I unsubscribe user from push notification in Unicast Notification scenario.


Solution

  • I found the way to unsubscribe from Unicast Notification. Not sure if this is the right way but it works for me. I used REST API Runtime Services

    The REST API for Push in the MobileFirst runtime environment enables back-end server applications that were deployed outside of the MobileFirst Server to access Push functions from a REST API endpoint.

    Thought it is designed for backend server it works for me.

    String  token = getToken("unregister-device");
    

    First get the token the details about how to get the token is here

    Once you get the token then implement the rest client check the documentation here

    Sample code.

    HttpClient httpClient = HttpClientBuilder.create().build();
            HttpDelete postRequest = new HttpDelete("http://localhost:10080/MyProject/imfpush/v1/apps/MyMobileApp/devices/12121-1212121-121212-12121");
                postRequest.addHeader("Content-Type", "application/json");
                postRequest.addHeader("Authorization", "Bearer "+token);
            HttpResponse response = httpClient.execute(postRequest);
            if (response.getStatusLine().getStatusCode() != 204) {
                throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
            String output;
            System.out.println("============Output:============");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }