0
votes

I am developing an iOS app using IBM MobileFirst Platform 8.0 sdk that have push notification capabilities

I managed to send notification via postman, the process was obtaining a token from the MobileFirst platform server and send the notification via rest api along with the token

The tutorial link i have follow is,

Server Side - https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/notifications/sending-notifications/

Client Side - https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/notifications/handling-push-notifications/cordova/

Get Token - https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/authentication-and-security/confidential-clients/#obtaining-an-access-token

For now, i am trying to send a push notification when the app triggers an adapter call

I am currently stuck on sending the notification using WL.Server.invokeHttp, below is more adapter code

function sendPushNotification(message) {    
    var result = getToken();    
    var access_token = "Bearer " + result["access_token"];    

    var requestStructure = {
        method : 'post',
        returnedContentType : 'json',
        path : '/imfpush/v1/apps/my.app/messages',
        headers: {
            "Content-Type" : "application/json",
            "Authorization" : access_token
        },  
        parameters : {
          'message':{'alert' : 'Test message'}
        }
    };

    result = MFP.Server.invokeHttp(requestStructure);

    return result;
}


function getToken() {
   var requestStructure = {
        method : 'post',
        returnedContentType : 'json',
        path : '/mfp/api/az/v1/token',      
        headers: {
            "Content-Type" : "application/x-www-form-urlencoded",
            "Authorization" : "Basic UHVzaE5vd213123asdsadGlvbjpQdXNoTm90aasdasdWZpY2F0aW9u"
        },
        parameters:{
            "grant_type" : "client_credentials",
            "scope" : "push.application.my.app messages.write"
        }
    };

    var results = MFP.Server.invokeHttp(requestStructure);
    return results;
}

I seems to having issue on

parameters : {
    'message':{'alert' : 'Test message'}
}

I might have done it wrongly on this part, do hope for advice.

Thanks in advance

1
You seem to be missing target in the JSON: ibm.com/support/knowledgecenter/SSHS8R_8.0.0/…Vivin K

1 Answers

0
votes

The sendPushNotification method use a wrong syntax for calling WS in Mobile First. you cloud change it like this:

function sendPushNotification(message) {    
var result = getToken();    
var access_token = "Bearer " + result["access_token"];    

var requestStructure = {
    method : 'post',
    returnedContentType : 'json',
    path : '/imfpush/v1/apps/my.app/messages',
    headers: {
        "Authorization" : access_token
    },
     body: {
        content: 'message':{'alert' : 'Test message'},
        contentType: 'application/json'
    }
};

result = MFP.Server.invokeHttp(requestStructure);

return result;

}