how to detect which element from UILocal notification was opened - ios

I am new to iOS
I want to get the index of the UILocal notification on which the user pressed View.
For example if i have 5 local notification on an app how and the users opened the 3 rd one how can I display programatically all the notifications and how to check which index of which notification did the user press?

Local notifications have no concept of an index. The index is representative of the fire date. When the user opens the app from a local notification, that notification is passed as a parameter so you can access the fire date and, probably more importantly, the userInfo.
All that said, you have no way to access the list of notifications that were being displayed in notification centre. If the user cleared old notifications you can't tell. You only know about the notification that the user was actually interested in and used to open the app.

Related

How to detect when user clears your app's notification

My app has notification feature. When I receive a notification I show a badge in my app icon.
As I know, when my app is on background, since user does not clicks on the notification at notification center, my app does not know that it has a notification. So it can not change the badge number.
Also from this topic:
Detect when a user clears notification from the notification center
If user clears the app's notification, the app can not detect it.
So the problem is here:
- My app is on background
- User receives a notification related to the app
- App adds a badge with number 1 to the app's icon
- User deletes the notification from notification center
- App never understands that notification has been removed so that it can remove the badge!!!
So how other apps fix this problem? Is there any solution for this?
There is no way to check when the user clears notifications for your app.
The usual practice for clearing the app badge is when the user has viewed the applicable content within your app (e.g. messages in a messaging app), or otherwise just clearing the badge the next time the user opens your app if this is not applicable, or you can't easily segment the notifications and connect them to viewable content in your app.
Note: you should not be relying on the user tapping on your notifications in order to get their content. If your user doesn't tap your notification, your app has no way of ever finding out its content, or that it ever existed.
Instead, when your app is opened, it should connect to your server to download updated content, then you can use this complete, accurate information to update your app badge as required. Do not try to fetch content from your notifications.
You can add custom action to your notification: "Mark as read" or "Delete". Remove badge in -application:handleActionWithIdentifier:... method.
Yes, it doesn't allow you to detect user cleans the notification. But there is no any way to do it. So I suggest a workaround to solve this problem.

Show Push Notification for time which user has set

I have implemented the Push Notification Successfully in the application, and I need to provide an option to user Where they can set the time for not showing receiving notification. all other time they can receive. I am storing the detail of their Do Not Disturb time in the database in the backend. I just want to check when the notification is fired. the current device time of user. and if that falls under their DND time. Notification is not to be shown and event the sound is not to be shown. Even in the Notification Section.
Any Help will be appreciated.
You can get the the timezone of the device along with the schedule. Depends on the time you need to fire the notification from your server.
i.e, once you fired the notification from your server you can't stop it from showing

How to handle badge count If user delete/clear the notification from the server in ios?

Currently I am implementing an ios app with the server push notification.
I am successfully able to send push notifications with badge.
Badge count will be incremented from the server. If user opens the application from notification, it will reset count on the server.
Now the query is if user is not opening application from notification and deleting/clearing all notifications.
That means there will be no notification so when app will be opened from the background it wont find any notification and so it will not reset the count on server and also not the badge value on app icon.
So how can i resolve this issue?
Use custom notification icon and maintain read and unread counts with in the app,you will not manage badge counts from push notifications when user remove and open notifications in the app.
From what I understand, the only way your app is getting the relevant data is if the user clicks on the notification. I'm assuming you're sending a custom payload of some kind. Though I don't know the nature of your app at all, I would say this is really not the way to go specifically because of the problem you're encountering - you have no way of knowing if/when the user clears notifications in the Notification Center and if they do, that data is forever lost to your app.
I would recommend your app request the new data from your server upon launch - ie/ get anything new since last time the app was opened - this way it's independent of whether the push was received or not and depending on what your server sends back, you can clear the push count (or not).
Whenever application hits appDidBecomeActive delegate, clear the badge count to zero.

iOS - change a view's content on push notification received event

I'm coming from Android development background. In Android you send a push notification and then you handle the rest using a service (creating the actual system notification, modifying app's content, etc).
As far as I know, when sending iOS push notifications, it automatically creates a system notification for you in the notification center based on your message payload. After searching here and on google, I think I have to use application:didReceiveRemoteNotification event to add/remove a view's content. How can I access the push notification's content so I can put it in the app as well (I want to display the message in the app even if the user has dismissed the notification in the notification center)? There will be another message later on (usually the same day) to remove the message from the app.
I understand that the application:didReceiveRemoteNotification method won't be called if the app is closed, so I will have to use some other event (like didfinishlaunching) to get the message. But how do I access the push notification's message itself?
I want to display the message in the app even if the user has dismissed the notification in the notification center
This can't be done. iOS passes the notification data to your app only if the user opens the app from the notification center. If the user dismisses the notification and later launches the app, the only way for the app to get this data is to retrieve in from your server.
As for getting the notification data if the app is launched from the notification center, see this question.

Deleting notifications from the notification center revisited

The previous posting on here regarding deleting notifications from the notification center claim its not possible to delete individual notifications, only all of them.
However individual notifications do get deleted for the reminder app - set 3 reminders to fire in a couple of minutes, when they fire go to the notification center, now select one, after the reminder app launches go back to the notification center and that specific notification has been deleted but others remain. So how is this achieved?
The Reminders app probably fires local notifications. Local notifications can be withdrawn, using cancelLocalNotification: on UIApplication.
(Additionally, push notifications when sent using the enhanced call (first byte is 1) supports an expiry parameter (when sending, not inside the JSON payload) that is supposed to mean that this notification, if not delivered by a certain date, should not be delivered. It is possible that this parameter is also used in a similar way to hide received notifications.
It is also highly possible that Apple's own apps do whatever the hell they want.)
When the user taps on the notification:
If the app was running in the background, you retrieve it using AppDelegate's method didReceiveLocalNotification.
If it wasn't running, then the notification can be obtained with the didFinishLaunchingWithOptions method. You just need to search the launchOptions dictionary for the UIApplicationLaunchOptionsLocalNotificationKey.
If you want to delete specific notifications which have already fired, specially when the user doesn't enter the app by tapping on the notification, then it's probably better to store them in NSUserDefaults so that you can still obtain them later. That approach is explained here.

Resources