How to get push notification status using Swift? - ios

In my app there is a setting to enable/disable push notification. Everything is fine with registering and my app is getting the notification.
Now I also unregister using UIApplication.shared.unregisterForRemoteNotifications() when I change in setting.
But when I try to fetch the status that app is already registered or not using UIApplication.shared.isRegisteredForRemoteNotifications then it always returns me true.
Any solution, how can I solve this?

I have implemented in this way in my app for push notification settings.
When user opt-out for push notification then I called an api(updateNotificaitonStatus with parameter "isEnabled = false") that disable the flag at server side for the push notification.
So whenever server has some data to send then...the server will check that is push notification enabled for the user?, if not, then just ignore to send the notification.
And if user changes his mind, and enabled the notification from app then agin an api call with the true flag(updateNotificaitonStatus with parameter "isEnabled = true").

Related

How do we know that user has enabled or disabled for notification service for our app in Swift?

I am using push notifications in my application using Firebase.
How do we know whether or not the user has enabled the notification service in his settings for our app?
You can know the Notification settings of the app by using the below code.
UNUserNotificationCenter.current().getNotificationSettings { settings in
print(settings.authorizationStatus.rawValue)
}
settings has whole lot of information about the notification permission of the app. Please find the Apple document here
As far as I know, there's no way you can alter push notification on the client-side,
unless you can make sure the app is running so you can filter out the notification.
But you can do some workaround on your backend. You can collect all the client token on your backend, and save whether the client is turning the push notification on or off. In this way, you can send the push notification only for the client who has enabled push notification.

Can I block receiving iOS Push Notifications during specified hours (night time) programmatically in Swift

my client wants to have an UISwitch control in Settings view in his app which is: 'Disable Notifications during night time'. He would like pushes that his API will be sending to mobile to be ignored if this option is turned on and it's a night time. Is this possible? I know that I can register and unregister for remote notifications, but this requires an App to be turned on. Is it possible to have it working like he wants?
The only way to do this is to have a configuration in the back-end. Notifications are sent from a server, and only handled in the app. There is no way, besides unregistering the phone from receiving notifications, to have the phone deny a notification in a certain time frame.
The user can set a time preference in the app, send it to the server, and have the server do a check so it only sends the notification to the user in the preferred time period.
Check out this 3rd party repo to send your push notifications and just write in your own check before pushing the notification.
https://github.com/nomad/houston
You can register or unregister for push notifications, i hope in your code switch you can make something like this:
let application = UIApplication.shared
//Didnt receive push
application.unregisterForRemoteNotifications()
//Receive push
application.registerForRemoteNotifications()
and if your client wants make this in Backend you can put a value on some database table with field receive:Bool, and the backend just take all the receive == true to send push notification on this devices

IOS Push Notification disable/enable on the basis of key

Just wondering whether there is any way to disable/enable a push notification when it is received.
For e.g :- When i receive a push notification then i first check in my app whether in notification setting i have enabled or disabled the notification.
There can also be multiple notification settings like
To disable a friend request notification
To disable message notification
So while sending a notification is there any way to append notificationType like if its for friend request or messaging.
Then after checking the notification type and its corresponding setting in the app, showing or discarding the notification.
You can't achieve this just in client side itself. Because once notification arrives it is handled by iOS and displayed in notification centre (or any other type as per user setting). App will not get the notification info, unless it is running.
You can have this as settings in Client and sync it with server to have a check there before pushing the notifications.

End-to-end tracking of ios push notification [duplicate]

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 start an iOS push notification from an in-app process

I'm building an app that plugs into a third-party service that will send messages to the iOS device. So far I've been unable to find any documentation on then starting off a push notification when the delegate method is fired to say that a new message has been received.
So far, I've got the app registering to receive push notifications and the delegate method firing, I'm just not sure how to connect the two together?
The app will have a minimum deployment of iOS 5.1 if that helps.
This is not how remote notifications work. Their main purpose is to notify application about some event. So application only receives remote notifications and note send them. So scenario is:
App is notified via
application:didReceiveRemoteNotification: //if running
or
application:didFinishLaunchingWithOptions: //if closed
According to notification payload you determine what exact action you need to perform. For example notification says that a new message was sent to the user. Then you need to send your custom request to the your server and get that new message.
I've discovered that in this case it is not push notifications that I want but local notifications instead.

Resources