2016-05-11 8 views
2

私はサイレントローカルプッシュ通知を実装する方法を探しています。そのユーザーが範囲外であるときに、私は静かな通知をユーザーに送信したい。iOSサイレントローカルプッシュ通知の目的c?

+0

ありません。サイレントローカル通知。 – Paulw11

+0

あなたが沈黙と言うとき、あなたはどういう意味ですか?あなたは音を避けたいですか?これをさらに説明してください。この質問は広すぎます。 – dokun1

+0

@ dokun1リモートのサイレント通知のようにバックグラウンドで動作する通知を送信しようとしています。ユーザーは通知センターで通知を表示しません。 –

答えて

6

解決済み。 ローカル通知を作成するときに、次の値を設定しないでください。このような

notification.alertBody = message; 
notification.alertAction = @"Show"; 
notification.category = @"ACTION"; 
notification.soundName = UILocalNotificationDefaultSoundName; 

だけクレートローカル通知:

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
notification.fireDate = [NSDate date]; 
NSTimeZone* timezone = [NSTimeZone defaultTimeZone]; 
notification.timeZone = timezone; 
notification.applicationIconBadgeNumber = 4; 
[[UIApplication sharedApplication]scheduleLocalNotification:notification]; 

これは、ローカル通知を送信するだけでアプリがバックグラウンドで動作しているとき、いかなる通知を通知センターに表示されません4としてIconBadgeNumber表示されます。 ViewController

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 

UNMutableNotificationContent *content = [UNMutableNotificationContent new]; 
//content.title = @"Don't forget"; 
//content.body = @"Buy some milk"; 
//content.sound = [UNNotificationSound defaultSound]; 
content.badge = [NSNumber numberWithInt:4]; 

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:15 repeats:NO]; 


NSString *identifier = @"UniqueId"; 
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier 
                     content:content trigger:trigger]; 

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
    if (error != nil) { 
     NSLog(@"Something went wrong: %@",error); 
    } 
}]; 

iOS10ため更新(UNUserNotificationCenter)

AppDelegate

@import UserNotifications; 

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 

UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge; 

[center requestAuthorizationWithOptions:options 
         completionHandler:^(BOOL granted, NSError * _Nullable error) { 
          if (!granted) { 
           NSLog(@"Something went wrong"); 
          } 
         }]; 

これは4

としてバッジカウントで15秒後にサイレント通知を送信します
+0

こんにちは@Ruturaj Desai:働いてくれてありがとうございます。しかし、私はUNUserNotificationCenterを使ってiOS 10と似ている必要があります。何か考えてください。 – Ammaiappan

+0

こんにちは@Ammaiappan、私はiOS 10(UNUserNotificationCenter)のための私の答えを更新しました。これはあなたを助けるかもしれません。ハッピーコーディング。 –

+0

こんにちは@Ruturaj Desai、私は同じことを試み、アプリケーションがフォアグラウンドでは完璧ですが、バックグラウンドでは動作しません。バックグラウンドを回避する方法はありますか? –

関連する問題