【问题标题】:Slient Push Notification not getting in iOS静默推送通知未进入 iOS
【发布时间】:2017-05-31 09:13:21
【问题描述】:

我正在尝试很多,但尚未成功在应用程序被终止状态时获得静默通知

这是我正在尝试的代码..

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];
}
}

【问题讨论】:

  • 查看application:didReceiveRemoteNotification:fetchCompletionHandler: 甚至application:didReceiveRemoteNotification: 的文档(讨论部分)建议实施另一个。
  • 但是当 {"aps":{"alert":"Enter your message","badge":1,"sound":"default"}} 工作正常,甚至终止状态时
  • @larme 你能详细说明一下吗.....
  • " 另外,如果您启用了远程通知后台模式,系统会启动您的应用程序(或将其从挂起状态唤醒)并在远程通知到达时将其置于后台状态。但是,如果用户强制退出应用,系统不会自动启动您的应用。在这种情况下,用户必须重新启动您的应用或重启设备,然后系统才会再次尝试自动启动您的应用。"
  • 我正在尝试很多但尚未成功在应用程序被杀死状态时获得静默通知老兄,很抱歉告诉你这个,但是,当应用程序被杀死时静默通知不起作用,无论你做什么都不会工作。

标签: ios objective-c silent-notification


【解决方案1】:

在负载的aps 字典中不得包含警报、声音或徽章键。

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

请试试这个。

【讨论】:

  • 我正在尝试使用 Slient 通知 @akshay .....那么我怎样才能添加警报和徽章键......并告诉你使用这种类型的有效负载的主要原因是在发送到通知之前更改我的警报正文
  • @SANTOSH 根据苹果文档:- 当向用户的设备发送静默通知时,iOS 会在后台唤醒您的应用程序并使其运行最多 30 秒。在 iOS 中,系统通过调用应用程序委托的 application:didReceiveRemoteNotification:fetchCompletionHandler: 方法来传递静默通知。使用该方法启动获取新数据所需的任何下载操作。在后台处理远程通知需要您为应用添加适当的后台模式。
【解决方案2】:

你应该像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];
}

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多