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
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;
}
Related
When I Hit ms graph API with my Nodejs code to send the email, an email is sent with no error displayed but the email is received in the outlook sent email which I'm using
I have proper accessToken what happing behind this please help here
Code below
const sendNotification = async (from, message) => {
const access_token = await getAuthToken();
try {
const response = await axios({
url: `${GRAPH_ENDPOINT}/v1.0/users/${from}/sendMail`,
method: "POST",
headers: {
Authorization: `Bearer ${access_token}`,
"Content-Type": "application/json",
},
data: JSON.stringify(message),
});
console.log("sendNotification status", response.statusText);
} catch (error) {
console.log(error);
}
};
This JavaScript Fetch code returns the data I need in Postman. But, I am trying to get this code to work in Google Script Editor for my google sheet, and it is giving me the following error:
ReferenceError: Headers is not defined line 3.
I am brand new to coding, so please bear with me.
Any idea what's wrong here?
POSTMAN CODE:
var myHeaders = new Headers();
myHeaders.append("Content-Type", "text/plain");
var raw = "{\n \"id\": 1,\n \"jsonrpc\": \"1.0\",\n \"method\": \"getbeaconbeststatedetail\",\n \"params\": []\n}";
var requestOptions = {
method: 'GET',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://mainnet.incognito.org/fullnode", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
EDIT:
I have tried to update this:
function myFunction() {
var data = {
'request': {
'id': '1',
'jsonrpc': '1.0',
'method': 'getbeaconbeststatedetail',
'params': []
}
};
var payload = JSON.stringify(data);
var options = {
'method' : 'GET',
'headers': { 'Content-Type': "text/plain", 'Accept': "text/plain"},
'muteHttpExceptions': true,
'contentType' : 'application/json',
'body' : payload
};
var url = "https://mainnet.incognito.org/fullnode";
var response = UrlFetchApp.fetch(url, options);
var txt= response.getContentText();
var d=JSON.parse(txt);
var sh1=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet14");
for (let i = 0; i < d.length; i++) {
sh1.getRange(1, 1).setValue(d);
}
}
But, I am still getting the following error:
Exception: Request failed for https://mainnet.incognito.org returned code 400
If you are trying to make an HTTP request from Google Apps Script, you have to use the UrlFetchApp() method.
As #Tanaike points out, GAS is based in JS but not all JS functions are allowed. However GAS does have many services to integrate with G Suite products, and external APIs or services. One very useful service is URL Fetch Service which allows you to perform any type of Http request.
For example:
// Make a GET request with headers.
var options = {
"method" : "GET",
"headers" : {
"Content-Type" : "text/plain",
"API_KEY": "dummydummy"
}
};
var response = UrlFetchApp.fetch("https://api.myawesomeapi.com/get", options);
You can also make POST or other requests. Please refer to the examples in the documentation for use cases.
Update:
You are receiving the 400 "Bad request" error from the "fullnode" endpoint because it requires a POST request with a payload.
function fetchFullNode() {
var url = "https://mainnet.incognito.org/fullnode";
var data = {
"jsonrpc":"1.0",
"method":"getblockchaininfo",
"params":"",
"id":65
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
var txt= response.getContentText();
return JSON.parse(txt);
}
I'm trying to create a custom IBM AppID Management Api interface in my application.
In order to do that, I'm using IBM IAM Token Manager library to get an IAM access token.
const itm = require('#ibm-functions/iam-token-manager')
const m = new itm({
"iamApiKey": apiKey
})
m.getAuthHeader().then(token => {
console.log("this one won't work", token)
}
var headers =
{
'accept': 'application/json',
'Authorization': token,
'Content-Type': 'application/json'
};
var options =
{
url: replacedIssUrl+"/users",
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
console.log(response)
if (!error && response.statusCode == 200) {
console.log(body); //returns "body: "Forbidden"
}
}
request(options, callback)
Whenever I try to pre-register a user with the library's generated token, the callback returns Status 403 - Forbidden, but if it gets the IAM Access token directly through ibmcloud shell (ibmcloud iam oauth-tokens), it works fine.
Does anybody have any clue why this is happening? I know for a fact that the IAM Token Manager library generated access token is working, because I'm using it to get the user ID on the same code.
When something is wrong with my Access Token, it usually returns "Unauthorized", not "Forbidden".
I have no clue why this is happening.
Thanks in advance.
When passing an IAM token in the headers, App ID expects it to be preceded by the "Bearer " string :
var headers =
{
'accept': 'application/json',
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
};
I have an Office 365 extension that provides its own "Send" button which does some custom processing of the "draft" email and sends it through other means (ie not Office 365 Web). Since the email isn't sent by Outlook it never makes it to the sent folder (which makes sense). I would like to take that draft email and move it to the sent folder and remove the draft flag so it looks like it was sent by Outlook 365 Web.
var getMessageUrl = Office.context.mailbox.restUrl + '/v2.0/me/messages/' + itemId;
var data = JSON.stringify({ 'ToRecipients': [{ 'EmailAddress': { 'Address': 'sheprts#cox.net', 'Name': 'Katie Jordan' } }], 'IsRead': true, 'IsDraft': false });
$.ajax({
url: getMessageUrl,
type: 'PATCH',
dataType: 'json',
headers: { 'Authorization': 'Bearer ' + accessToken, 'Content-Type': 'application/json' },
data: data
})
})
.done(function (item) {
})
.fail(function (error) {
var err = ""
});
The request above works fine except the changing of the draft flag. If this isn't the solution what else can I do? I need to get a draft email into the sent folder as "Not" a draft.
Unfortunately you cannot create non-draft messages using the API.
Change your "other means" and set the "SaveInSentFolder" flag; or change to SMTP for your "other means".
So we are using Firebase to send notifications to our users on our iOS app. It works great in the console, but now we are looking to allow a few more people to send notifications, and we don't really want them signing into Firebase to do that. One solution we came up with is to put a form on our website and let them sign in and send notifications from there, but we can't get it to work. The code below is a stripped down version of what we have right now. It uses the cloud messaging API to send an HTTP POST request to a specific topic and Firebase responds with a message_id, however no message is delivered on any of the devices.
<!DOCTYPE html>
<html>
<body>
<input id="title"></input>
<input id="message"></input>
<button>Submit</button>
</body>
<script src="https://code.jquery.com/jquery-1.12.2.min.js" integrity="sha256-lZFHibXzMHo3GGeehn1hudTAP3Sc0uKXBXAzHX1sjtk=" crossorigin="anonymous"></script>
<script>
$("button").click(function () {
var title = $("#title").val();
var message = $("#message").val();
var param = {
"to" : "/topics/myTopic",
"content_available" : true,
"notification" : {
"title" : title,
"body" : message
}
};
$.ajax({
url: 'https://fcm.googleapis.com/fcm/send',
type: "POST",
headers: { 'Authorization': 'key=<myKey>', 'Content-Type': 'application/json' },
data: JSON.stringify(param),
success: function () {
alert("success");
},
error: function(error) {
alert("error: "+error);
}
});
});
</script>
</html>
Solution:
After we played around with it a bit more, we found that content_available should really be false in order to actually display a notification in our app. We also made sure to implement jQuery variables correctly. You can see our improved script for handling everything below.
$("button").click(function () {
// Grab the input values
$title = $("#title").val();
$message = $("#message").val();
// Verify that the message is not left empty
if ( $message.length < 2 ){
// Message is empty
return;
}
// Create the JSON object
var paramaters = {
"to" : "/topics/test",
"content_available" : false,
"notification" : {
"title" : $title,
"body" : $message
}
};
// Send request
$.ajax({
url: 'https://fcm.googleapis.com/fcm/send',
type: "POST",
headers: { 'Authorization': 'key=$key', 'Content-Type': 'application/json' },
data: JSON.stringify(paramaters),
success: function (e) {
// Process succeeded
console.log(e);
},
error: function(error) {
// Display error message
}
});
});
We also made sure that our server keys were not included directly in our code as Frank mentioned in his comment. Everything now works and we are successfully receiving notifications.