iOS 애플리케이션 : 알림을 지우는 방법?
일부 푸시 알림이 전송되는 iOS 응용 프로그램이 있습니다. 내 문제는 메시지 / 알림이 탭 된 후 iOS의 알림 센터에 남아 있다는 것입니다. 다음에 응용 프로그램을 열 때 알림 센터에서 응용 프로그램의 알림을 제거하려면 어떻게해야합니까?
사람들이 setApplicationIconBadgeNumber
알림을 지우기 위해 제로 값을 부르는 게시물을 보았습니다. 그것은 나에게 매우 이상하게 보입니다. 그래서 다른 해결책이 있다고 생각합니까?
편집 1 :
알림을 지우는 데 문제가 있습니다. 여기 내 코드를 참조하십시오 :
- (void) clearNotifications {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions != nil)
{
NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary != nil)
{
NSLog(@"Launched from push notification: %@", dictionary);
[self clearNotifications];
}
}
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
NSLog(@"Received notification: %@", userInfo);
[self clearNotifications];
}
Xcode를 통해 앱을 실행하고 있습니다. 앱이 최소화되고 알림 센터의 알림을 사용하여 앱을 시작하면 로그 didReceiveRemoteNotification
에서을 호출하고 중단 점을 사용하여 볼 수 있음을 알 수 있습니다 clearNotifications
. 그러나 여전히 알림은 알림 센터에서 중단됩니다. 왜?
Notification Center는 비교적 새로운 기능이므로 Apple은 알림을 지우는 데 완전히 새로운 패러다임을 강요하고 싶지 않았습니다. 대신에, 그들은 [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
상기 통지를 지우기 위해 다목적 이었다. 조금 이상해 보일 수도 있지만, Apple은 앞으로 더 직관적 인 방법을 제공 할 수도 있지만 당분간 공식적인 방법입니다.
나 자신, 나는이 발췌 문장을 사용한다 :
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
알림 센터에서 앱의 모든 알림을 지우지 못합니다.
Just to expand on pcperini's answer. As he mentions you will need to add the following code to your application:didFinishLaunchingWithOptions:
method;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
You Also need to increment then decrement the badge in your application:didReceiveRemoteNotification:
method if you are trying to clear the message from the message centre so that when a user enters you app from pressing a notification the message centre will also clear, ie;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
It might also make sense to add a call to clearNotifications in applicationDidBecomeActive so that in case the application is in the background and comes back it will also clear the notifications.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self clearNotifications];
}
Update for iOS 10 (Swift 3)
In order to clear all local notifications in iOS 10 apps, you should use the following code:
import UserNotifications
...
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
center.removeAllDeliveredNotifications() // To remove all delivered notifications
} else {
UIApplication.shared.cancelAllLocalNotifications()
}
This code handles the clearing of local notifications for iOS 10.x and all preceding versions of iOS. You will need to import UserNotifications
for the iOS 10.x code.
If you have pending scheduled local notifications and don't want to use cancelAllLocalNotifications
to clear old ones in Notification Center, you can also do the following:
[UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
It appears that if you set the scheduledLocalNotifications
it clears the old ones in Notification Center, and by setting it to itself, you retain the pending local notifications.
If you're coming here wondering the opposite (as I was), this post may be for you.
I couldn't figure out why my notifications were clearing when I cleared the badge...I manually increment the badge and then want to clear it when the user enters the app. That's no reason to clear out the notification center, though; they may still want to see or act on those notifications.
Negative 1 does the trick, luckily:
[UIApplication sharedApplication].applicationIconBadgeNumber = -1;
In Swift I'm using the following code inside my AppDelegate:
func applicationDidBecomeActive(application: UIApplication) {
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
}
Maybe in case there are scheduled alarms and uncleared app icon badges.
NSArray *scheduledLocalNotifications = [application scheduledLocalNotifications];
NSInteger applicationIconBadgeNumber = [application applicationIconBadgeNumber];
[application cancelAllLocalNotifications];
[application setApplicationIconBadgeNumber:0];
for (UILocalNotification* scheduledLocalNotification in scheduledLocalNotifications) {
[application scheduleLocalNotification:scheduledLocalNotification];
}
[application setApplicationIconBadgeNumber:applicationIconBadgeNumber];
When you have repeated notifications at future, you do not want to cancel those notifications, you can clear the item in notification center by:
func clearNotificationCenter() {
UIApplication.sharedApplication().applicationIconBadgeNumber = 1
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
You cannot clear notification when your app is open in the foreground by calling the method below immediately after receiving local notification, otherwise you will receive tens of hundreds of notifications. Maybe because the same notification apply again, and now is the time to fire, so you keep fire, apply again, fire, apply....:
[UIApplication sharedApplication].scheduledLocalNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
When you logout from your app, at that time you have to use a below line of code on your logout button click method.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
and this works perfectly in my app.
You need to add below code in your AppDelegate applicationDidBecomeActive
method.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
Got it from here. It works for iOS 9
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
}
참고URL : https://stackoverflow.com/questions/8682051/ios-application-how-to-clear-notifications
'program story' 카테고리의 다른 글
UIAlertController 사용자 정의 글꼴, 크기, 색상 (0) | 2020.08.06 |
---|---|
Angular에서 객체 반복 (0) | 2020.08.06 |
HTML5 캔버스에 점 그리기 (0) | 2020.08.06 |
DIV를 가로 및 세로 가운데에 (0) | 2020.08.06 |
java.lang.IllegalArgumentException : AppCompat가 현재 테마 기능을 지원하지 않습니다 (0) | 2020.08.06 |