How do I deregister device in MFP - ibm-mobilefirst

I am sending push notification via an external script and capturing the response that is return from MobileFirst. The response is always 200 and a messageId is in the response JSON object
How can I simulate a error condition?
I used the MFP API to remove the subscription, removing the device from the device tab in the MFP console. However, I can still send and receive push notification for that deviceID .

Unsubscribing from the tag subscription (which you have subscribed in the code) does not clear all subscriptions. A default Push.ALL tag subscription stays in the DB. This is why you are able to still send notifications.
You can remove the device registration either using the SDK ( as mentioned by Gaurab) or use the REST API call to do this.
Details here: Push Device Registration Delete

I assume that you are using IBM MobileFirst v8.0.
You need to implement these API in client side to unregister the device or unsubscribe from tags.
Unregister the device from push notification service instance.
MFPPush.unregisterDevice(
function(successResponse) {
alert("Unregistered successfully");
},
function() {
alert("Failed to unregister");
}
);
Unsubscribe from tags.
var tags = ['sample-tag1','sample-tag2'];
MFPPush.unsubscribe(
tags,
function(tags) {
alert("Unsubscribed successfully");
},
function() {
alert("Failed to unsubscribe");
}
);

Related

Twilio programmable chat message user friendlyName

I am using Twilio Programmable Chat to add a chat feature to my mobile app built in React Native. I'm using the JS client SDK for this.
When the app receives a new message, the data that comes through uses the user identity for the author field. Is there away to include the friendlyName in the payload so I can display this to the user.
I could make a separate request for all users and find the correct user within the app but it would be great if this data could just be on the same request.
Thanks for any help
Yes, only author field present in new message event, I used an alternate approach
channelMembersDict = {}
// Assuming you have set selected an Channel
this.activeChannel.getMembers().then(members => {
members.forEach(mem => {
//member contains friendlyName attribute
this.channelMembersDict[mem.state.identity] = mem;
//If you really want user then
mem.getUser().then(user => {
this.channelMembersDict[mem.state.identity] = user;
});
});

Sending a link using push notifications in Azure for Android

I have created a backed in asp.net web-forms successful send a notification for all registered devices but I tired to send a link for example I need to send one push notification include a link When user touch on the notification redirect to mobile browser and open the link.
private static async void SendNotificationAsync()
{
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString("<Here is my endpoint >", "<hub name>");
await hub.SendGcmNativeNotificationAsync("{ \"data\" : {\"message\":\"Hello Users \"}}");
}
Based on my experience, we could use custom json format and at receiver end convert that string into url. For example,Here’s a sample of the complete JSON payload with a custom "web_link" key containing the URL:
{"data": "{\"message\":\"xxxx\",\"web_link\":\"https://www.example.com\"}"}
Then we could override the OnMessage function to create Notification, we also could get more detail info from the Azure document.

PushNotificationIOS.requestPermissions() callback

I don't want to ask my users to allow notifications before they actually need them in the app.
So when a user schedules a local notification in my app I want to request notification permissions and if the user accepts then set the local notification.
The problem is that there doesn't seem to be any callback for PushNotificationIOS.requestPermissions(), meaning that if I call PushNotificationIOS.checkPermissions() right after it will run before the user has tabbed the alert window and return 0 in the permissions object, even though the user might accept.
So my question is if there is any way to request permissions and subsequently set a notification, or do I have to request permissions before I actually need to use them?
There is the option to add an event listener for when a device registers for push notifications.
PushNotificationIOS.addEventListener('register', this._onPushNotificationRegistration);
When you attempt to schedule your local notification you can check the permissions at that point and if you don't have permission yet, you could request them.
_prepareNotification(alertBody, soundName, badge) {
let notification = {
alertBody: alertBody,
applicationIconBadgeNumber: badge,
fireDate: new Date(Date.now() + (1000 * 10)).getTime(), // 10 seconds in the future
soundName: soundName
};
PushNotificationIOS.checkPermissions((permissions) => {
if (permissions.alert) {
this._scheduleNotification(notification);
} else {
this._requestNotificationPermissions(notification);
}
});
}
When you request permissions, store the notification that you want to send in your state.
_requestNotificationPermissions(notification) {
this.setState({
notificationToPost: notification
});
PushNotificationIOS.requestPermissions();
}
When the user allows you to send them notifications, then schedule it in the registration response.
_onPushNotificationRegistration(token) {
console.log('Registered for notifications', token);
if (this.state.notificationToPost) {
this._scheduleNotification(this.state.notificationToPost);
}
}
This is a rough example of how you might achieve what you require, I am sure that there are nuances around you application state that this doesn't cover, but hopefully it will give you some ideas.
I have put some of these ideas into a sample application that you can have a look at https://github.com/AidenMontgomery/react-native-sample.
The release notes for v0.28-rc has just been published and one of the new features is that PushNotificationIOS.requestPermissions() has been promisified, which is exactly what I needed. See the commit here.

How to get mobile reistration ID using GSM Titanium?

I am developing android app in titanium,by button click event I have to get registration ID from GCM. how to do that I am new to titanium.
I followed this http://iamyellow.net/post/40100981563/gcm-appcelerator-titanium-module, but I am not able to understand how to implement?
Thanks in advance.
To implement push notification, you should use Ti.CloudPush module.
You can achieve GCM Push Notification in 6 easy steps
Setting up Google Cloud Messaging
Push Configuration in ACS Console
CloudPush Module Implementation
Retrieve Device Token
Cloud User Login
Subscribe a Channel
1. Setting Up GCM
To use GCM, you need to create a Google API project to obtain a Google API key and GCM sender ID. For instructions on creating and obtaining these items, see Android Developer: Getting Started with GCM and follow the directions in "Creating a Google API Project", "Enabling the GCM Service" and "Obtaining an API Key".
We will use our Project number as GCM Sender ID.
When creating a new server key, you are asked for a list of IP addresses to accept requests from. Do not enter any information in the textbox and click Create to accept all IP addresses.
2. Push Configuration in the ACS
Go to your apps, then go to My Apps -> Manage ACS -> DEVELOPMENT -> Settings/Android Push Configuration and enter your Google API key in the Google Cloud Messaging (GCM) API Key textbox and GCM sender ID in the Google Cloud Messaging (GCM) Sender ID textbox. (which we got from Step 1)
3. CloudPush Module Implementation
Add CloudPush module into your application.
To add CloudPush module, you need to add <module platform="android">ti.cloudpush</module> in your TiApp.xml. Then require the module in your javascript file using var CloudPush = require('ti.cloudpush');.
To use push notifications, in the tiapp.xml file, you need to specify push type to GCM.
To do this go to your tiapp.xml.
Add/edit the following lines
<property name="acs-push-type-development" type="string">gcm</property>
<property name="acs-push-type-production" type="string">gcm</property>
<property name="acs-push-type" type="string">gcm</property>
4. Retrieve Device Token
Call the retrieveDeviceToken method before setting the enabled property to true to enable the device to receive push notifications. You must call retrieveDeviceToken before any other CloudPush API call or else the device will not receive push notifications.
var CloudPush = require('ti.cloudpush');
var deviceToken = null;
CloudPush.retrieveDeviceToken({
success: function deviceTokenSuccess(e) {
Ti.API.info('Device Token: ' + e.deviceToken);
deviceToken = e.deviceToken;
},
error: function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
5. Cloud User Login
Before subscribe for Push Notification, cloud user should logg in.
Cloud.Users.login({
login: username,
password: password
}, function (e) {
if (e.success) {
alert("login success");
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
6. Subscribe a Channel
You need to subscribe to a channel to get the pushnotification. Push notification will be sending to the particular channel and it will be reached to all users who subscribed to the channel.
if(deviceToken != null){
Cloud.PushNotifications.subscribe({
channel: 'yourchannelName',
device_token: deviceToken,
type: 'gcm' //here i am using gcm, it is recommended one
}, function (e) {
if (e.success) {
alert('Subscribed for Push Notification!');
} else {
alert('Subscribe error:' + ((e.error && e.message) || JSON.stringify(e)));
}
});
} else {
alert('You need to retrieve the device token first');
}
Now you can send push notification. To do this go to My Apps -> Manage ACS -> DEVELOPMENT -> Push Notifications, here you can see number of clients subscribed for push notification, channels etc. You can send the push notification from there.
UPDATE : Attention!!
GCM supports devices that run Android 2.2 and later
Google Play Store application should be installed in your device.
For pre-4.0 devices, the user is required to set up their Google account.
You're using an android device for testing, not emulator(Since you can't install Google Play in your emulator).
Google play service is running on your device.
The following links will help you:
Titanium.CloudPush
Android SDK Titanium
ACS Push Notification Using GCM

GCM server side implementation for java

I need to implement a standalone application for the server side of gcm to push notifications to the device. Is there any reference i could get other than the one on the Getting started page.People say something about xmpp. Do we need to use this or can we directly use the gcm server side methods.Help.Or is there any other easy way to implement this.I hope i put my question properly.
Here is nice tutorial for GCM server side implementation for java.
URL: java gcm server side implementation
Example code: java gcm server side implementation`{
new Thread(){
public void run(){
try {
//Please add here your project API key: "Key for browser apps (with referers)".
//If you added "API key Key for server apps (with IP locking)" or "Key for Android apps (with certificates)" here
//then you may get error responses.
Sender sender = new Sender("AIzaSyB7Ej255tpTaemk_-Ljmn4GcklldT14Hp4");
// use this to send message with payload data
Message message = new Message.Builder()
.collapseKey("message")
.timeToLive(3)
.delayWhileIdle(true)
.addData("message", "Welcome to Push Notifications") //you can get this message on client side app
.build();
//Use this code to send notification message to a single device
Result result = sender.send(message,
"APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA",
1);
System.out.println("Message Result: "+result.toString()); //Print message result on console
//Use this code to send notification message to multiple devices
ArrayList<String> devicesList = new ArrayList<String>();
//add your devices RegisterationID, one for each device
devicesList.add("APA91bEbKqwTbvvRuc24vAYljcrhslOw-jXBqozgH8C2OB3H8R7U00NbIf1xp151ptweX9VkZXyHMik022cNrEETm7eM0Z2JnFksWEw1niJ2sQfU3BjQGiGMq8KsaQ7E0jpz8YKJNbzkTYotLfmertE3K7RsJ1_hAA");
devicesList.add("APA91bEVcqKmPnESzgnGpEstHHymcpOwv52THv6u6u2Rl-PaMI4mU3Wkb9bZtuHp4NLs4snBl7aXXVkNn-IPEInGO2jEBnBI_oKEdrEoTo9BpY0i6a0QHeq8LDZd_XRzGRSv_R0rjzzZ1b6jXY60QqAI4P3PL79hMg");
//Use this code for multicast messages
MulticastResult multicastResult = sender.send(message, devicesList, 0);
System.out.println("Message Result: "+multicastResult.toString());//Print multicast message result on console
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}`
The simplest way to implement GCM server side for Java is using restful POST.
URL: "https://android.googleapis.com/gcm/send"
Example code: using scribe framework as consumer
public void pushToAndroidDevice(String deviceToken, String data) {
OAuthRequest request = new OAuthRequest(Verb.POST, "https://android.googleapis.com/gcm/send");
request.addHeader("Authorization", "key=" + apiKey);
request.addHeader("Content-Type", "application/json");
request.addPayload(data);
Response response = request.send();
}
There are 2 ways you can implement server for GCM connections
1) XMPP
2) HTTP
The difference being XMPP allow you to get response back from device to server(Bidirectional) and HTTP is (Unidirectional) for GCM, you can only send push notification to device.
In case you need the full implementation of Java Client and HTTP server, here is the link
GCM Client and Server