How to get the device token in iOS 10 if user does not accept to receive Push Notifications? - ios10

I had my app for iOS 9 implementing application(_:didRegisterForRemoteNotificationsWithDeviceToken:) that gets invoked whether or not user accepts to receive Push Notifications, hence I get and store on the remote server the device token in both cases.
Now with iOS 10 this delegate method never gets called if user does not accept to receive Push Notifications... Is there any other way to get the device token in such a case?

You can't. If user doesn't accept notifications, you can't get any token for this.

This is the fact when user not accept to receive notification then application will not generate device token or sometime by mistake click on don't allow option, so whenever you want to need app required Push notification need to show any popup(Alert view) message there for why you need push notification access inside app then second time give some tutorial/advice/guidance for how user enable manually push notification service from iOS device Setting option.

Related

Can I delete a push notification from a device which have receive this push notification?

I have the following problem.
I look for a capability to delete a push notification from a device (iPhone respectively Apple Watch) but this device have receive the push notification.
The use case is that a USER A send a request to all available USER Bs. A push notification is send to all these USER Bs. At the moment the push notification arrive it will shown on their Apple Watch. One of the USER Bs answer that he accept this request. After he press the button to accept the request the notification should be deleted on all the other devices.
I really do not know a capability to do this.
But I know that you can handle this problem in Android as provide a unique ID for this notification. After that you can delete the other notifications with this particular ID.
Is their maybe a same way for iOS like it is provided in Android?
This doesn't really have anything to do with WatchKit/Apple Watch.
To answer your main question: no, you can't do this as it describe. Once you fire off a notification, it's in the user's hands to decide what they want to do with it.
As an alternative, you could maintain an "inbox" with every notification in your app and use the notification to prompt the user to check it. That way you could remove a notification from the inbox on the server side of things.

How can I send apple push notifications based on userIDs not only on device tokens?

I use push notifications in my iOS application but the problem is - several users with different IDs in the system can use the same device and I want to send push notification only to the user which is logged in, but now notifications arrive to the device even if other user is logged in. I know that APNS identifies pushes only by device tokens...
But is there some way to send push notifications based on user ID or other information not only device token?
You'll have to manage it yourself.
Whenever a user logs-in to your app, notify your server (send the user-id to the server).
Do the same whenever a user logs-off.
In your server, based on the currently logged-in user (you'll have to manage a database that contains for each device token the currently logged-in user), you can decide
which push notification to send to the device.
I don't think iOS push notification is right for your problem. here is why
consider you have an app with multiple users using the same device. you have a simple scenario where users can assign tasks to other users and they would like to get notifications when a task has been assigned to them
you do 'user to device token' mapping yourself on the server. consider user A and user B. both registered to receive notifications. so on the server both of them will have an entry in the mapping of what their device tokens are
Now, user B is logged out -- you will update on the server by removing the token for user B? or say use a flag to update the status that he is logged out?
user A now assigns a task to user B. on the server, you can see that user B has no mapping or mapping status is not active?
what happens to the notification?
you will end up queuing a notification for user b until he logs in? and push again? but again how do you know when to push again?
It is better you provide some UX in the app to get the notifications on a tap or periodically poll the server for any notifications

any option to know if apple app get the push notification?

I build xcode app that get push notification, the main problem is that the push notification is very critical for me.
so I want to check if the push notification is delivered to the device with the app installed, I understand that if the iphone dosn't have internet connecction / 3G the push notification is not getting to the device.
how can I check if the device get the notification or not?
how can I check if the APNS successful to deliver the push notification?
I want to send sms if the push notification is not deliver to the device so I think about the idea to get the notification event when it's open by the push notification, and to send request to my server so i can know if the push notification is successful deliver or not. the main problem is that the user need to open the app every time he get the notification and in the night it's a problem. so this option is not good for me.
I check the feedback server push notification but i don't find any info that I can get if the push notification is delivered or not
any idea??
With iOS7 you have a new method called
application:didReceiveRemoteNotification:fetchCompletionHandler:
which you probably could use for your task. From Apple's Docs:
Implement this method if your app supports the remote-notification background mode.
...
When a push notification arrives, the system displays the notification to the user and
launches the app in the background (if needed) so that it can call this method. Use this
method to download any data related to the push notification. When your method is done,
call the block in the handler parameter.
Unlike the application:didReceiveRemoteNotification: method, which is called only when
your app is running, the system calls this method regardless of the state of your app.
The short answer, you can't, since APNS is one way. However, since an app can execute arbitrary code upon receipt of a notification, you can use this to say, send an http request to your own server when the notification is recieved.
There are any number of reason why push notifications might not get delivered to your user, or might not be delivered in a timely manner. Apple does not provide any mechanism for you to query the status of a push notification that you have sent.
If your app is currently running on the user's device and the user is accepting notifications for your app, you can implement the following method in your app delegate. It would be called whenever a push notification is received and in this method you could send a request back to your server to indicate the message was received. However this will only work while the user is running your app.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
In general though, it sounds like you'e relying on push notifications for something you shouldn't. From Apple's Local and Push Notification Programming Guide:
Important Because delivery is not guaranteed, you should not depend on
the remote-notifications facility for delivering critical data to an
application via the payload. And never include sensitive data in the
payload. You should use it only to notify the user that new data is
available.
There is no way to find out whether the notification was delivered to the device or no. APNS is a one way service. If there is no internet connection on the device then the APNS server will hold the last notification for some period of time which is no specified by Apple. If a new notification is sent to APNS for delivery then the old notification data is lost and replaced by the new data if its undelivered. If the notification is delivered then also the old notification data is deleted on the APNS server.
Please go through the following link : Apple Push Notification
Hope this helps you...........
If you are using JAVAPNS to send the APNS notification, you can use the below:
List<PushedNotification> notifications =
Push.combined("alert", badge, "default", "cert.p12", "certpassword", true, deviceToken);
for (PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
//Push is successful. Do your thing...
}
else {
//Push is not successful. Do your thing...
}
}

How to determine Push notification status programmatically?

In my app, for first time push notification registration, I call didRegisterForRemoteNotificationsWithDeviceToken and save the device token in persistence as well as update my server list for device token. Now afterwards if somebody turns the push notification settings off from iPhone Settings how can I determine it from my app so that I can remove the device token from server as well. I know APNS provides a feedback list, but other than that is there a way to determine it in App programmatically? Thanks for any help!
I believe you do not want to manage tokens this way.
Your app should always be asking Apple for an APNs token. You should always then send that token to your own server, likely associating the token with your user (if you have one). You do this because the token could change, so you want to make sure you always have up-to-date tokens.
The Feedback service will tell you (actually, you poll it at some interval of your choosing) which tokens have become invalid. At this point, you remove the tokens from your server-side database. To be clear, you need a server-side process that polls Apple's feedback service and then updates your server-side database.
You will not receive feedback about invalid tokens until you try to send a notification using the token. The notification will (I believe) be accepted by Apple when you send it, but when Apple discovers it's for an invalid token, the message is dropped, and the token is added to your feedback.
Now, if the user of your app accepts push notifications when your app first asks about it, but later turns off notifications via the Settings app for your app, you will not get any feedback about it. What happens, near as I can tell, is that any notification you send to that device will be sent to the device, but the OS drops it, honoring the user's ultimate choice in the Settings app for your app and notifications.
Finally, there is an API you can call in your app to get a bitmask of which kinds of notifications are enabled for your app on the device. Here's a method I wrote for this purpose; adjust as needed:
+(BOOL)acceptsPushNotifications
{
UIRemoteNotificationType mask = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (mask & UIRemoteNotificationTypeAlert) == UIRemoteNotificationTypeAlert;
}
But I would not recommend using this to decide if you app should tell your server to delete the token from your database. That's not how the whole APNs system is intended to work... I believe.

How can I control the Apple Push Notifications I receive

I have successfully created a server that sends Apple Push Notifications, and my iphone receives them.
For example I have Notification of type A and notifications of type B,
How can I control the types of notifications I received on the iphone side? For example I only want type A and not B (Just like Facebook, I want notifications for Friend Request, but not for Walls)
Thanks
you cannot stop your device on receiving a specific type of notification unless it is done on server side. Though you can ignore a notification when app is in running state as you get the notification in didReceiveRemoteNotification and you can simply ignore it after checking it but if app is in background or it is closed then you cannot control the incoming notifications from within your app.
you can make a service on server to set preferences for notification types.
from device, user can enable/disable the push service for individual features and update the preferences on server from device.
On server, before sending the PUSH, you can check for the preferences selected by user from the table and send only those notifications which the user has opted for.