且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS的推送通知的问题

更新时间:2023-02-26 21:22:24

从苹果公司的本地和推送通知编程指南


  

处理本地和远程通知


  
  

让我们回顾一下可能出现的情况时,系统提供本地
  通知或应用程序的远程通知。


  
  

      
  1. 当应用程序没有在前台运行的通知交付。


      
      

    在这种情况下,系统presents该通知,显示一个
      警报,徽章图标,也许播放声音。


      
      

    由于$ P $的结果psented通知,用户点击的动作
      警报或抽头(或点击)按钮的应用程序图标。


      
      

    如果操作按钮被窃听(运行iOS的设备上),系统
      启动应用程序和应用程序调用其委托的
      的application:didFinishLaunchingWithOptions:方法(如果
      实施);它通过在通知有效载荷(用于远程
      通知)或本地通知对象(本地
      通知)。


      
      

    如果应用程序图标运行iOS的设备上拍了拍,应用程序调用相同的方法,但配料没有信息
      有关通知
    即可。如果应用程序图标被点击一
      运行OS X的计算机,应用程序调用委托的
      的applicationDidFinishLaunching:方法,其中委托可以
      取得远程通知的有效载荷。


  2.   
  3. 当应用程序在前台运行的通知交付。


      
      

    应用程序调用其委托的
      应用程序:didReceiveRemoteNotification:方法(用于远程
      通知)或应用程序:didReceiveLocalNotification:方法
      (用于本地通知),并将在通知有效载荷或
      本地通知对象。


  4.   

块引用>

所以你的情况,当应用程序在后台运行,当您单击通知/警报,操作系统将您的应用程序到前台。所以它陷入第二点。

您可以实施应用程序:didReceiveRemoteNotification:方法来获取通知的有效载荷,如果操作按钮被窃听。但是,当应用程序图标pssed代替动作信息$ P $,通知有效载荷不与方法转发。你唯一的选择是与您的服务器,并同步数据。毕竟根据苹果的政策,推送通知只是告诉有在服务器上的数据,你需要连接到服务器并获取数据的两种方式。

I am doing a project in which push notification feature is one of the key feature.
It is working fine when I am in the app, I receive notification and handle that notification.

But the issue is when I am in background and notification receives I see badge on my app icon and when I click on the icon my app is launching but the didReceiveRemoteNotification method is not called so I am unable to handle that notification.

And another issue is some times it shows the notification message in device notification list and some times it didn't .

When I am entering in my app through clicking on notification list item the didReceiveRemoteNotification calls and I am successfully able to handle notification. I write following code in didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

NSDictionary* remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif != nil)
{
    NSLog(@"didFinishLaunchingWithOptions\nNotification recieved:\n%@",remoteNotif);

    notificationData=[[NSDictionary alloc]initWithDictionary:remoteNotif];
    [notif saveNotification:remoteNotif];
}

Help me to resolve this . Thanks in advance.

From Apple's Local And Push Notification programming guide

Handling Local and Remote Notifications

Let’s review the possible scenarios when the system delivers a local notification or a remote notification for an application.

  1. The notification is delivered when the application isn’t running in the foreground.

    In this case, the system presents the notification, displaying an alert, badging an icon, perhaps playing a sound.

    As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon.

    If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

    If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.

  2. The notification is delivered when the application is running in the foreground.

    The application calls its delegate’s application:didReceiveRemoteNotification: method (for remote notifications) or application:didReceiveLocalNotification: method (for local notifications) and passes in the notification payload or the local-notification object.

So in your case, when application is running in background, and when you click the notification/alert, operating system brings your app into foreground. So it falls under second point.

You can implement application:didReceiveRemoteNotification: method to get the notification payload, if action button is tapped. But when the application icon is pressed instead of action message, the notification payload is not forwarded with the method. Your only option is to contact your server and sync the data. After all as per Apple's policy, push notification only tells that there is data on server, and either way you need to connect to server and get the data.