Java 当应用程序处于后台时,当我单击通知项(在通知栏中)时,应用程序调用了哪个方法?
我在互联网上搜索发现:在iOS7之后,即使某个应用程序被杀死,如果用户单击与该应用程序关联的通知,该应用程序也会调用此方法:Java 当应用程序处于后台时,当我单击通知项(在通知栏中)时,应用程序调用了哪个方法?,java,ios,notifications,Java,Ios,Notifications,我在互联网上搜索发现:在iOS7之后,即使某个应用程序被杀死,如果用户单击与该应用程序关联的通知,该应用程序也会调用此方法: - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
我使用的是一个临时配置文件,应用程序在后台,并没有被杀死
我的代码如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// JPush
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo]; // handle the notification
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"%@--userinfo", userInfo);
NSLog(@"%ld--applicationState", (long)[UIApplication sharedApplication].applicationState);
public static void pushIos(List<String> registrationIds,Map<String,String> map){
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.registrationId(registrationIds))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(map.get("title"))
.incrBadge(1)
.setSound("happy.caf")
.addExtra("pushId", map.get("pushId"))
.addExtra("pushType",map.get("pushType"))
.addExtra("title", map.get("title"))
.build())
.build())
.setMessage(Message.content("123"))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
try {
PushResult result = jPushClient.sendPush(payload);
System.out.println(result);
}catch(APIConnectionException e){
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
当我测试时,我在设备日志中什么也没有得到
编辑-1
在应用程序中注册APN的我的代码:didFinishLaunchingWithOptions
:
我的iphone操作系统版本是10.2
// 7.push regist
if (IOS10) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"succeeded!");
}else {
NSLog(@"error:%@", error);
}
}];
} else if (IOS8_10){//iOS8-iOS10
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {//iOS8以下
[application registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
}
编辑-2 参考Chandan的评论,如果将
content available=1
添加到aps
字典中,我们可以唤醒应用程序,但是如何将值添加到aps
使用java
:
代码如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// JPush
// IOS 7 Support Required
[JPUSHService handleRemoteNotification:userInfo]; // handle the notification
completionHandler(UIBackgroundFetchResultNewData);
NSLog(@"%@--userinfo", userInfo);
NSLog(@"%ld--applicationState", (long)[UIApplication sharedApplication].applicationState);
public static void pushIos(List<String> registrationIds,Map<String,String> map){
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.registrationId(registrationIds))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(map.get("title"))
.incrBadge(1)
.setSound("happy.caf")
.addExtra("pushId", map.get("pushId"))
.addExtra("pushType",map.get("pushType"))
.addExtra("title", map.get("title"))
.build())
.build())
.setMessage(Message.content("123"))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
try {
PushResult result = jPushClient.sendPush(payload);
System.out.println(result);
}catch(APIConnectionException e){
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
publicstaticvoidpushios(列表注册ID、地图){
PushPayload payload=PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(观众.注册ID(注册ID))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(map.get(“标题”))
.incrbage(1)
.setSound(“happy.caf”)
.addExtra(“pushId”,map.get(“pushId”))
.addExtra(“pushType”,map.get(“pushType”))
.addExtra(“标题”,map.get(“标题”))
.build())
.build())
.setMessage(Message.content(“123”))
.setOptions(Options.newBuilder()
.setApnsProduction(真)
.build())
.build();
试一试{
PushResult结果=jPushClient.sendPush(有效负载);
系统输出打印项次(结果);
}捕获(APIConnectionException e){
e、 printStackTrace();
}捕获(APIE){
e、 printStackTrace();
}
}
看起来您正在尝试接收远程推送通知。您是否记得注册远程推送通知?此外,还需要启用推送通知功能
基本上,有三个步骤
请参见试试这些代码行,我想它们会解决您的问题 这是注册远程通知并处理它们的代码 UnuseNotificationCenterDelegate添加此委托
if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog( @"registerForPushWithOptions:" );
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
[[UIApplication sharedApplication] registerForRemoteNotifications]; // required to get the app to do anything at all about push notifications
NSLog( @"Push registration success." );
}
else
{
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
}
}];
}
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permiossion granted");
}
else {
NSLog(@"Permission Granted");
}
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void
(^)(UIBackgroundFetchResult))completionHandler
{
// iOS 10 will handle notifications through other methods
NSLog(@"Notification payload is %@", userInfo);
if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"10.0" ) )
{
NSLog( @"iOS version >= 10. Let NotificationCenter handle this one." );
// set a member variable to tell the new delegate that this is background
return;
}
// custom code to handle notification content
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( @"INACTIVE" );
completionHandler( UIBackgroundFetchResultNewData );
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( @"BACKGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
else
{
NSLog( @"FOREGROUND" );
completionHandler( UIBackgroundFetchResultNewData );
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {
}];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSLog( @"Handle push from foreground" );
// custom code to handle push while app is in the foreground
NSLog(@"%@", notification.request.content.userInfo);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSLog( @"Handle push from background or closed" );
// if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background
NSLog(@"%@", response.notification.request.content.userInfo);
}
Application:didReceiveMemotentification:
仅当您将值为1的键添加到通知有效负载中时,才会在后台调用
当应用程序位于前台时,您可以检查可用的内容
NSString* contentAvailable = [NSString stringWithFormat:@"%@", [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"]];
仅供参考:-这是Xcode8+iOS10的一个bug,NSLog根本不显示。你可以查看这个链接@Chandan,抱歉,兄弟,这不是由你说的错误引起的,我也可以找到其他NSLog信息。如果我没有错,我们在使用临时配置文件时无法调试。那么它将如何显示NSLog。@Chandan,请看我的这篇文章:application:DidReceiveMemotentification:只有当您将值为1的内容可用键添加到通知负载中时,才会在后台调用。当然,我是这样做的,这样我可以接收远程通知,然后我可以单击推送通知栏的项,是吗?你能显示步骤2的代码吗?如果出现错误,可能会导致您的应用程序未正确接收推送通知。谢谢您的回答,我的意思是单击通知项重新进入我的应用程序,我的应用程序未通过-(void)application:(UIApplication*)application didReceiveMemotentification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult)completionHandler方法,我还测试了您的anser。我的应用程序没有调用我发布的此方法。请尝试实现上述代码,我相信您不会遇到任何问题。我已经测试了此代码