Twilio multiple push credential sid - ios

I'm trying to implement Twilio swift quick start app, anyway I want to configure the app on both platforms iOS and Android,
I reached step 8 in the iOS tutorial
and I have a new PUSH_CREDENTIAL_SID for the iOS from the APN type, while the Android app is from the FCM type already have a PUSH_CREDENTIAL_SID that is being used in the config server file.
$ACCOUNT_SID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$API_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$API_KEY_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$PUSH_CREDENTIAL_SID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
$APP_SID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
How can I Add multiple PUSH_CREDENTIAL_SIDs for both apps ? or is there another way to figure this out ?
Thanks in advance

In your code you can have only 1 push_credential_sid.
In order to use the android AND ios at the same time, you should go into your project using this link: https://www.twilio.com/console/notify/services
Create a new service, and you will be able to create a service with the serviceID for android and IOS.
It will provide you a new serviceID to (SID) to use in your backend code.
And it should work for both platform :)
source : https://www.twilio.com/docs/notify/configure-ios-push-notifications#configure-your-twilio-service-to-use-your-apns-credentials

From the backend server, it only can config one push service id in 'PUSH_CREDENTIAL_SID', but if you check the backend code logic, then we can find it just use these information and create a JWT for the APP. So for the accessToken API, APP can pass one more parameter push_sid (the current parameter is identity), backend server will parse this push_sid from the APP request, but do not read from env config any more.
Android will pass the FCM push service id and iOS will pass the APN service id, so one server can support both Android and iOS.

You can control through IF/ELSE logic by passing DEVICE TYPE parameter.
public function createTwilioToken($deviceType = 'ANDROID')
{
$twilioAccountSid = config('app.TWILIO_ACCOUNT_SID');
$twilioApiKey = config('app.TWILIO_API_KEY_SID');
$twilioApiSecret = config('app.TWILIO_API_KEY_SECRET');
$conversationId = config('app.TWILIO_CONVERSATIONS_SERVICE_SID');
$androidPushCredentialSid = config('app.TWILIO_PUSH_ANDROID_CREDENTIAL_SID');
$iosPushCredentialSid = config('app.TWILIO_PUSH_IOS_CREDENTIAL_SID');
// choose a random username for the connecting user
$identity = 'USER-IDENTITY';
// Create access token, which we will serialize and send to the client
$token = new AccessToken(
$twilioAccountSid,
$twilioApiKey,
$twilioApiSecret,
86400,
$identity
);
// Create Chat grant
$grant = new ChatGrant();
$grant->setServiceSid($conversationId);
if ($deviceType == 'IOS')
{
$grant->setPushCredentialSid($iosPushCredentialSid);
}
else if ($deviceType == 'ANDROID')
{
$grant->setPushCredentialSid($androidPushCredentialSid);
}
// Add grant to token
$token->addGrant($grant);
return $token->toJWT();
}

Related

My android project crashed when I called token.jwt for twilio chat

I am trying to generate access token for twilio chat but got this error:I have been trying to figure out where the error is coming from but can't get it figured out. I will really appreciate your help. Thanks
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zihron.projectmanagementapp, PID: 16355
java.lang.Error: javax.xml.datatype.DatatypeConfigurationException: Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl not found
at javax.xml.bind.DatatypeConverterImpl.<clinit>(DatatypeConverterImpl.java:744)
at javax.xml.bind.DatatypeConverter.<clinit>(DatatypeConverter.java:78)
at javax.xml.bind.DatatypeConverter.printBase64Binary(DatatypeConverter.java:547)
at io.jsonwebtoken.impl.Base64Codec.encode(Base64Codec.java:24)
at io.jsonwebtoken.impl.Base64UrlCodec.encode(Base64UrlCodec.java:22)
at
io.jsonwebtoken.impl.AbstractTextCodec.encode(AbstractTextCodec.java:31)
at io.jsonwebtoken.impl.DefaultJwtBuilder.base64UrlEncode(DefaultJwtBuilder.java:314)
at io.jsonwebtoken.impl.DefaultJwtBuilder.compact(DefaultJwtBuilder.java:282)
at com.twilio.jwt.Jwt.toJwt(Jwt.java:100)
at ZihronChatApp.token.TokenGenerator.getToken(TokenGenerator.java:34)
at com.zihron.projectmanagementapp.ChatActivity.onCreateView(ChatActivity.java:43)
I have my details below:
public AccessToken getToken() {
// Required for all types of tokens
String twilioAccountSid ="AC601f2c7***7ed***640***264c***d0d";
String twilioApiKey = "SK684***dda***c81****6c4a****093**";
String twilioApiSecret ="96****dbc06****b74d50***b9***3*4";
String serviceSid="IS***a29****e24****5d****4b20**3e*";
String identity = "joshua.hamilton#gmail.com";
ChatGrant grant = new ChatGrant();
grant.setServiceSid(serviceSid);
AccessToken token = new AccessToken.Builder(twilioAccountSid,
twilioApiKey, twilioApiSecret)
.identity(identity).grant(grant).build();
Log.e("++==--",""+token.toJwt());
//.identity(identity).grant(grant);
return token;
}
Twilio developer evangelist here.
The Twilio Java library is not intended for use within Android projects.
The issue here is that you should not be storing your credentials within your application. A malicious user could decompile your application, take your credentials and abuse them.
Instead, you should create a server (or use some sort of serverless environment, like Twilio Functions) that can run this code and return the token. You should then make an HTTP request from your Android application to fetch that token. Check out the Twilio Programmable Chat Android Quickstart to see how it's done there.

Cannot set PubNub auth key Swift iOS

I have a webapp and the iOS app built in Swift. The thing is I don't know Swift and I'm trying to modify the iOS app in order to add the authorization key to the PubNub client before subscribing/publishing to channels.
Link to PubNub docs
PRE:
Access Manager is enabled
my_auth_key is hardcoded and already enabled form the server for the corresponding channel I want to subscribe.
Here is the code
What's the correct way to set the auth key?
Thanks in advance
Polak, mentioned docs code snippet refer to previously created instance to get it's pre-configured PNConfiguration instance and change required field. This practice can be used in case if you need change something at run-time.
If you have data for authKey at the moment of client initialization, you can set it:
var pubnubClient: PubNub = {
let configuration = PNConfiguration(publishKey: UCConstants.PubNub.publishKey, subscribeKey: UCConstants.PubNub.subscribeKey)
configuration.authKey = "my_auth_key"
return PubNub.clientWithConfiguration(configuration)
}()
Also, I've tried exact your code and don't have any issues with setting of authKey, because I can see it with subscribe request.
If you still will have troubles with PAM and auth key usage, please contact us on support#pubnub.com
Looks like you can:
let configuration = PNConfiguration(
authKey: "super_secret",
publishKey: UCConstants.PubNub.publishKey,
subscribeKey: UCConstants.PubNub.subscribeKey
)
Based on the Obj-C code: https://github.com/pubnub/objective-c/blob/8c1f0876b5b34176f33681d22844e8d763019635/PubNub/Data/PNConfiguration.m#L174-L181

Twilio configuration Profile SID

I am using the Twilio Video API. In my node.js I used this code,
var grant = new VideoGrant();
It requires configurationProfileSid but I can't find docs on how to get it?
I think it is a capability token.
But how can I get it using twilio node js?
Or is there any other way to get it?
Twilio developer evangelist here.
Configuration Profiles used to be required, but have been deprecated. So you no longer need the configurationProfileSid. You can give access to particular rooms though.
Here's an example Node.js application that generates access tokens for a Video application. The important part is the route that generates the token:
app.get('/', function(request, response) {
// Create an access token which we will sign and return to the client,
// containing the grant we just created
var token = new AccessToken(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_API_KEY,
process.env.TWILIO_API_SECRET
);
// Assign identity to the token
token.identity = request.query.identity || 'identity';
// Grant the access token Twilio Video capabilities
var grant = new VideoGrant();
grant.room = request.query.room;
token.addGrant(grant);
// Serialize the token to a JWT string
response.send(token.toJwt());
});
This documentation on access tokens should help too.

Update device data on IoT for Automotive Starter Application using Bluemix iOS Client api

I have successfully configured a Bluemix backend vehicle monitoring app described in this tutorial:
http://www.ibm.com/developerworks/library/mo-connectedcar-app/index.html.
I have configured and ran the tester app successfully. I have also successfully initiated BMSClient on iOS( after adding mobile access service to the backend app), using the following code:
BMSClient.sharedInstance.initialize(bluemixAppRoute: backendURL, bluemixAppGUID: backendGUID, bluemixRegion: BMSClient.Region.usSouth)
let mcaAuthManager = MCAAuthorizationManager.sharedInstance
mcaAuthManager.initialize(tenantId: tenantId)
BMSClient.sharedInstance.authorizationManager = mcaAuthManager
BMSClient.sharedInstance.authorizationManager = MCAAuthorizationManager.sharedInstance
Here is a snippet of how tester app updates the information:
var id = $("#prop_id").val();
var property = $("#property").val();
var value = $("#value").val();
var payload = {
id: id,
property: property,
value: value
};
var group = id.split("-")[0];
var num = id.split("-")[1];
var message = new Messaging.Message(JSON.stringify(payload));
message.destinationName = "iot-2/type/"+window.config.iot_deviceType+"/id/"+id.split("-")[0]+"/cmd/setProperty/fmt/json";
$("#statusMessage").html("Published command!<br><b>Topic: </b>" + message.destinationName + "<br><b>Payload: </b><pre>" + JSON.stringify(payload, null, 4) + "</pre>");
$("#statusMessage").css("display", "block");
client.send(message);
Now I want to update manage(update properties, and/or send message to) the simulated vehicles via iOS app. The thing is that I am unable to find an API to do this on iOS.
Do I need to develop a backend API also? If no, on iOS, how can I get an API to do this?
Also here are URLs:
Backend app: http://alvi-app.mybluemix.net/
Tester app URL to update properties: http://alvi-app.mybluemix.net/tester
You will need to develop a custom backend API to accept messages for updating properties, etc. sent from your iOS app.
You can send a REST request using the BMS Core libraries from your iOS app to your backend. Then you can have your backend process these requests to update properties, etc.
https://github.com/ibm-bluemix-mobile-services/bms-clientsdk-swift-core
The Core SDK has an example of a REST request in the README.

Silent Authentication in Actionscript without generating the dialog box

I need to send the device token required for push notification in iOS to urban-airship server from the actionscript code. How can i do that? I am using their API's in my app. I am just using urban-airship to test push notifications on my app.
Since their url requires user authentication, I want to write code so that authentication happens silently without generating the pop dialog.
I finally figured out myself how to solve this problem:
// AIR developer can send the tokenId received in event.data to his server via URL request
var urlRequest:URLRequest;
var urlLoader:URLLoader = new URLLoader();
var tokenId:String; //tokenId received after registering for push notifications
var urlString:String = "https://go.urbanairship.com/api/device_tokens/" + tokenId;
urlRequest = new URLRequest(urlString);
urlRequest.authenticate = true;
urlRequest.method = URLRequestMethod.PUT;
URLRequestDefaults.setLoginCredentialsForHost("go.urbanairship.com",<userId>,<password>) ;
urlLoader.load(urlRequest);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);

Resources