Migrating Parse Push to AWS - ios

I am having an issue with Parse Push on AWS. We have the adapter configured per these specifications and can seem to broadcast to one specific user (don't know how or why that user) using the curl method, but I am confused how (or if) I can use this to send user to user push notifications. For example, "X liked Y's Z" Where X is the liker, Y is the liked and Z is the object being liked.

in parse you have multiple options to send push notifications.
Push notifications can be sent to one or more users by providing a query with all the installations that you like to send the push to. You can also send push notifications for specific channel that the user subscribed to, this is very good for marketing or maybe if your app have different type of users (for example: sellers, buyers etc.)
The reason that a push is being sent to installations and not to users is because that one users can have multiple installations (e.g. iphone,ipad,other device etc.)
like i said there are multiple options to send push but i recommend to send a push using one of the following approaches:
From cloud code - you can create cloud code function that will be triggered by the client and this cloud code function will first create a query of all the installations that you need to send the push to and will execute the function that will actually send the push. This cloud code function can receive multiple parameters that can contain any data that needs to be processed before you sending the push, such data can be array of users, channel name and more.
the following cloud code snippets show how to send a push for all users who successfully subscribed and have device token:
Parse.Cloud.afterSave("SendPush", function(request) {
var query = new Parse.Query(Parse.Installation);
query.exists("deviceToken");
var payload = {
alert: "after save push"
};
Parse.Push.send({
data: payload,
where: query
}, {
useMasterKey: true
})
.then(function() {
response.success("Push Sent!");
}, function(error) {
response.error("Error while trying to send push " + error.message);
});
});
and then from your iOS SDK you call this cloud code function in the following way:
NSDictionary * parameters = # {}; // put parameters if required
[PFCloud callFunctionInBackground: #"SendPush"
withParameters: parameters block: ^ (id _Nullable object, NSError * _Nullable error) {
// callback result
}
];
From iOS SDK - if you don't want to send your push from cloud code you can do it directly from your iOS. I think it's better to do it in cloud code because in cloud code you write ones and then you can trigger this function from any SDK and also from the REST API.
In Parse docs you can find a lot of examples on how to send push from iOS SDK all the examples can be found here:
http://parseplatform.github.io/docs/ios/guide/#push-notifications
but like i said the best is to do it with cloud code.

Answer is similar if you're using AWS services without Parse. A better architecture is where the mobile device invokes business logic in the cloud (i.e., in an AWS Lambda function) and that code sends the push notifications. This allows you to build a more secure solution because you can control content and control who can send to whom (i.e., you must be on someone's friend list to send) within your business logic. If you open permissions to publish directly from the device, then you make system vulnerable to attacks where someone uses the app's credentials and publishes harmful content (potentially to all your app's users).
Example of sending push notification from an AWS Lambda function...
Can you send SNS push notification from lambda function in Amazon AWS?

Related

Swift - register and re-register push notifications

Im trying to find a way to de-authorise users for push notifications when they log out from an app Ive found this function UIApplication.shared.unregisterForRemoteNotifications() which aparently works however I never see notifications being disabled, I also read in the documentation that it should not be used often or something to that effect, I basically want to have a toggle button in my app where the user clicks it one way and gets the standard enable notifications popup and another way to disable notifications on the fly, Im not a native swift developer so any pointers welcome
Also is it possible to attach a callback to this to know if it executes successfully, Im trying the following but get the error Argument passed to call that takes no arguments
UIApplication.shared.unregisterForRemoteNotifications() { (result, error) in
if let error = error {
call.error("Error", error)
} else if let result = result {
call.success([
"deregister": true
])
}
}
Edit: I found this which says its not possible to toggle on and off Change push notifications programmatically in Swift
With that in mind does this mean that the standard for devices is that:
1) when a user log out of their account they can still receive notifications.
2) When a user creates a new account on the same app it uses the same token and so receives notifications from the old account ?
3) when a user sells their phone and another guy/gal downloads the same app that they will receive notifications from that other users account (in terms of 3rd party push service one signal, aparently you dont need to refresh the player id)

Send notification to everybody but user who triggered it?

I'm using Firebase Messaging to send out notifications to users of my iPhone app. My database is structured like this:
- Users
- user1
- user2
- Groups
- group1
- members
- user1
- user2
When a user joins a group they get subscribed to a topic corresponding to that group. I have a cloud function that listens for writes in that group, and sends a notification to the groups topic when a write happens:
exports.sendNotifs = functions.database
.ref('pets/{petId}/events/{eventId}').onWrite(event => {
const pet_Id = event.params.petId;
const payload = {
'notification': {
'title': `${toTitleCase(name)} just logged an event`,
'body': `${events[eventType]} for ${toTitleCase(petName)}`,
'sound': 'default',
}
};
admin.messaging().sendToTopic(pet_Id, payload);
});
However, this results in everybody getting a notification including the person who did the write that triggered the notification. I only want other people in the group to display a notification since the triggering user doesn't need to see one. I tried appending the sending user's uid as extra data of the notification and only displaying the notification if the recieving user's uid doesn't match the notification data's uid. This works when the application is in the foreground but not if its in the background, so if the user writes then closes the application before he receives the notification it'll display for him when he receives it, something I'm trying to avoid.
How can I make sure only other members of a group get a notification? Are messaging topics not good for this?
If you use Topics it's not possible to send to everyone except one.
If you are Ok sending to everyone, and then filtering on the client, you will need to use the data messages, and not notification messages, to avoid the problem with background/foreground you described.
https://firebase.google.com/docs/cloud-messaging/concept-options
i guess that the solution is:
each iOS client from group1 will subscribe to topic /topics/group1.selfUserId
the server already have the list with all users that are part of group1
the iOS client ask server to send notification to group1.members beside selfUserId (make a http post request)
server send gets all group1.members beside selfUserId and send notification to all /topics/group1.memberX
As already mentioned, you cannot exclude someone who has been registered to a Topic.
So what you can do is sending a message to a single device. So for example you have a group of ten persons, one person posts a message, you have to send nine single messages to the other nine persons of the group.
What you need to do is to store the registration token of every single user into your database and you have to take into account that registration tokens will change after some time.

How do I use Parse.com's push notification feature for a change in PFObjects?

Can I use Parse.com’s push notification feature to send an array of PFObjects to the user when either a new one is added, or an existing one is deleted?? If so, how should I go about it? I’m writing an app for iOS. Any help would be much appreciated.
Edit:
After doing a bit of searching, I see I have to use the cloud code aftersave() method, and then send a push notification through JavaScript.
If anyone could provide examples of how to go about it, that would be great. In the meantime I'm going through the docs. Thanks again all.
The Parse Documentation really contains everything you need to get started. Writing the code that sends the actual notification is only a small part of the whole process that is required to send push notifications. Here are the steps required:
Create an SSL Certificate: create the cert via Apple Developer Center then export it with Keychain Application
Create Provisioning Profile: also done via Apple Dev Center.
Configure Parse App: Change Parse settings, then upload the cert you created in step 1.
Configure App in Xcode: Change app to use provisioning profile you created in step 2.
Add Code to Prompt user for Notifications: write the code in your app that asks a user if they want to receive Push notifications, then store their device ID in Parse Installation class.
Send the Notification: as mentioned earlier this is the easiest step of all. It can be done via the Parse Dashboard (Click on Push notifications when looking at the Data Browser), via the Parse API or via Cloud-code. I did it via Cloud-Code and it looks like this:
// Find the Installation (i.e. iOS Device) that belongs to my user*
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', user);
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "This is the notification message",
badge: "Increment"
}
}).then( function(){
console.log("Push notification successfully sent!");
});
*I added a field in my installation class that captures a user. That way I know which device belongs to which user and can send targeted Push Notifications.
Prepare for Release: When you release your app, you will need to do some additional configuration to get everything ready to release.
Again, I recommend the Parse Docs. They are comprehensive and very helpful. Let me know if you have any additional questions. Hope this helps!
** BTW: If you don't have any experience with cloud-code, then you can complete steps 1-5, then manually send the notifications via the Parse Dashboard.

react-native: push notifications + parse

I am currently working on a small app project to learn and try out react-native on iOS. I have some experience with parse (parse.com) and would love to integreate parse in the new app. Currently, I have no problems including parse js into react-native. I am able to log in with accounts etc. Now I need to send push notifications to a certain number of users (not all users).
What I don't understand is how push notifications should work with react-native and parse. Usually, I would connect a device installation with a user ID and then send a push to a certain number of users (which means to the devices with the corresponding installation). The react-native guide (https://facebook.github.io/react-native/docs/pushnotificationios.html#content) doesn't mention anything like that. And even though it gives the parse guide as a reference, I fail to see how I should be able to send pushes via parse. The guide leaves a lot of information to be desired too. To what source do these "Listeners" subscribe to? From which server am I going to send notifications etc?
As I understand, parse js is not able to read the current installation. I hesitate to add Parse iOS to the project too. This feels unnatural and shouldn't be a required thing to do although it would allow me to read the current installation. (but still parse js is not able to register that installation in order to subscribe to push notifications).
At this point, I feel a little bit lost. This piece of information (https://news.ycombinator.com/item?id=9271687) tells me that it should be possible somehow. I just can't figure out how :(
Hope someone can help me with that. Some advice would be truely appreciated.
EDIT: react-native implements this behavior by default now. The interesting part is the event listener for the register event which now returns the device token. Procedure is pretty straight forward now. Just have a look at the docs Also check out the answer by JWindey. Has some very important points in them that are needed to actually trigger the events.
After a while and a lot of trying things out, we came up with an answer today. This is our solution and it seems to be working pretty good.
We use the following resources:
react-native
parse-js
Parse Rest API
react-native-remote-push (https://github.com/darylrowland/react-native-remote-push)
Follow the parse guide for push notifications (https://parse.com/tutorials/ios-push-notifications) and get everything set up correctly (profiles, certificates etc.). Using the react-native-remote-push component later on, you don't have to follow the steps 5 and 6.
Now add react-native-remote-push to you project. We had to make some minor adjustments to the code (mainly dealing with legacy objC code), but that may depend on your own project.
Our project has some kind of "starting page" that is shown every time the app is opened. On this page, we deal with push notification permissions as well as with the registration of the device token and the listener for push notifications. Our goal is to mimic the same behavior that we would receive with the parse iOS SDK.
We need to register the device and subscribe to a push channel first. react-native-remote-push allows us to handle the permissions and receive a device token. We then proceed to use this device token to register this installation via the Rest API. This code is part of our componentDidMount() call.
var PushManager = require('./RemotePushIOS');
var registerInstallation = require('./Installation');
componentDidMount() {
PushManager.requestPermissions(function(err, data) {
if (err) {
console.log("Could not register for push");
} else {
registerInstallation({
"deviceType": "ios",
"deviceToken": data.token,
"channels": ["global"]
});
}
});
PushManager.setListenerForNotifications(this.receiveRemoteNotification);
}
PushManager is the required component from react-native-remote-push and registerInstallation is the function containing the Rest API call.
/**
* registers an installation
* data should look like the following:
* {
* "deviceType": "ios", // or "android"
* // if android is targeted set
* // "pushType": "gcm",
* // "GCMSenderId": "56712320625545", // whatever the later means
* "deviceToken": "29e32a686fd09d053e1616cb48",
* "channels": [
* ""
* ]
* };
* for more information visit:
* https://www.parse.com/docs/rest#installations-uploading
*/
var registerInstallation = function(data) {
var url = "https://api.parse.com";
url += "/1/installations";
fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'X-Parse-Application-Id': PARSE_APP_ID,
'X-Parse-REST-API-Key': PARSE_REST_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(processStatus)
.then(parseJson)
.catch(error);
};
module.exports = registerInstallation;
"processStatus", "parseJson" and "error" are just some small functions that handle the result of the API call. I can provide more details if necessary. This function allows us to add a lot of information via the "data" object, such as userid, app version, parse version etc., just as you're used to from the iOS SDK. We only have a basic example working right now, but it should be easy to extend on this basis. This step was very important for us, because we need to associate every installation with a certain user.
You should be able to receive push notifications by now. You can handle them in a "receiveRemoteNotification" function that acts as a listener. A basic function is provided on the website of the react-native-remote-push component.
I hope I could share some insight about this topic. If I should go into more detail on certain parts, I'll gladly add some more information.
I did some investigation with the Parse + react-native combination and have it working.
You have to add the Parse SDK (follow the guide) to your project, and link all the necessary libraries.
Don't forget to add the steps on point 5: https://parse.com/tutorials/ios-push-notifications.
Then add RCTPushNotificationManager.h + m (from react-native/Libraries) to your project. After that, add the following in AppDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[NSNotificationCenter defaultCenter] postNotificationName:#"RemoteNotificationReceived"
object:self
userInfo:userInfo];
}
That should do it.
The official PushNotificationIOS has register event, which we can obtain token from. So, with having #MrMuetze's REST API, I could install the device to Parse.
PushNotificationIOS.addEventListener('register', function(token){
registerInstallation({
"deviceType": "ios",
"deviceToken": token,
"channels": ["global"]
})
});

How does an iOS device identify itself to server without user log in

I am writing an iOS app with a Rails API backend. The Rails backend will serve JSON data to the app. I have the following requirements.
The app will be a free download
The app will show data on a map
The app will show data in the vicinity of the user
Upon loading the app the device should send some unique identifier to the server identifying itself as a device that is running this app.
There will be no authentication for the user as it is not required. The data is available to anyone who downloads the app. All the server needs to know is that the client is a device running the app. The server cannot serve data to any other client
I would like to run the data using SSL between the device and server
The user location will be sent to the server and the server returns the corresponding pieces of data that are in the vicinity of the user
The client receives the JSON and caches the data locally.
Question: Given these requirements, how to set up steps 4 & 5?
Also: If I want to search more on this topic what keywords should I be googling for?
Consider using OpenUDID or SecureUDID.
I give you 2 options.
First of all, the easy way. From some time, apple forbids access to the device ID. However, they give you a device token instead.
To get this unique token, the user must register for remote notification.
Upon application launching, call the following function:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Then this callback will be called:
- (void)application:didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken.
Send the token to your server and you're done. Problems with this approach are obvious. Your user will have to register for remote notification.
Another approach is to use the MAC address of the wi-fi board.
To do this:
IPAddress.h
IPAddress.c
Import this files into your project.
Then use this function:
InitAddresses();
GetHWAddresses();
for (int i=0; i<MAXADDRS; ++i)
{
//There is a way you can obtain more info about the hw_addrs, but in general, it's the first.
NSLog(#"MAC: %s", hw_addrs[i]);
}
FreeAddresses();
Create a hash using the mac address above and you're done.
Hope it helps.
Upon first launch, the app sends a request to the server saying Hi, I'm a new client, give me an id! The server generates a new, random id and sends it back. The app saves the id locally and uses it henceforth to uniquely identify itself.

Resources