2017-05-31 132 views
-2

没有得到我想要很多,但没有成功还没有得到通知无声时,应用程序被杀害状态Slient推送通知iOS中

下面的代码我想..

APS数据:

{ 
    "aps": { 
      "content-available": 1, 
      "sound": "" 
     } 

} 


    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
NSDictionary *userInfo1 = userInfo; 
NSLog(@"userInfo: %@", userInfo1); 

//self.textView.text = [userInfo description]; 
// We can determine whether an application is launched as a result of the user tapping the action 
// button or whether the notification was delivered to the already-running application by examining 
// the application state. 

if (application.applicationState == UIApplicationStateActive) 
{ 
    //opened from a push notification when the app was on background 

    NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]); 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was Running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    [alertView show]; 


} 
else 
{ 
    // a push notification when the app is running. So that you can display an alert and push in any view 

    NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]); 
     [self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo]; 
} 
} 
+0

查看'application:didReceiveRemoteNotification:fetchCompletionHandler:'或甚至'application:didReceiveRemoteNotification:'的doc(讨论部分),它建议实现另一个。 – Larme

+0

但是当{“aps”:{“alert”:“输入您的消息”,“badge”:1,“sound”:“default”}}工作正常,甚至处于死亡状态 – SANTOSH

+0

@larme可以请您详细说明...... “ – SANTOSH

回答

0

你应该实现在AppDelegate.m

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ 
    NSDictionary *aps = [userInfo objectForKey:@"aps"]; 
    NSLog(@"hello"); 
    NSLog(@"userinfo---->>>>%@",userInfo); 
    [UIApplication sharedApplication].applicationIconBadgeNumber=[[aps objectForKey:@"badge"] integerValue]; 
    [self application:application didReceiveRemoteNotification:userInfo]; 
} 
+0

谢谢..我试过这个不是succ – SANTOSH

2

在有效载荷的aps字典不得包含警报,声音或徽章密钥。

{ 
    "aps":{ 
     "content-available" : 1 
    } 
} 

请试试这个。

+0

我试着用Slient通知@akshay .....然后我怎么才能添加警报和徽章键.......并告诉你使用这种类型的主要原因的有效负载是在发送通知之前更改我的警报主体 – SANTOSH

+0

@SANTOSH按照苹果文档: - 当向用户的设备传递无声通知时,iOS会在后台唤醒您的应用程序,并让其运行长达30秒。在iOS中,系统通过调用应用程序提交静默通知:didReceiveRemoteNotification:fetchCompletionHandler:您的应用程序委托的方法。使用该方法启动获取新数据所需的任何下载操作。在后台处理远程通知需要您为应用程序添加适当的背景模式。 –