即使应用程序没有运行,也可以显示警报

3

enter image description here我正在开发一个应用程序,希望即使应用程序未运行,用户也能收到通知。就像在Uber司机应用程序中,用户会收到接受/拒绝乘车请求的提示一样。使用VoIP和PushKit可以实现吗?哪种方法最好?


你需要在iOS中使用通知。有两种类型:1. 远程通知 2. 本地通知。请查看此链接 https://developer.apple.com/notifications/。 - Gagan_iOS
你已经回答了自己的问题,是的,你应该使用推送通知。我猜你真正想问的问题是什么是最好的推送通知方式? - torinpitchers
@Gagan_iOS。非常感谢。推送通知不是我需要的那个。正如我所提到的,要求就像Uber司机应用程序一样。用户甚至在应用程序未运行时也可以看到警报,这与普通推送通知不同。 - jkdev
你可以选择本地通知,它可以满足你的需求。查看此链接以获取有关本地通知的更多信息:https://code.tutsplus.com/tutorials/an-introduction-to-the-usernotifications-framework--cms-27250 - Gagan_iOS
@Gagan_iOS 再次感谢。我认为PushKit和VoIP可以一起使用。https://developer.apple.com/library/content/samplecode/Speakerbox/Introduction/Intro.html。 - jkdev
是的,你可以选择那个。这对你来说是 PN。 - Gagan_iOS
1个回答

3

使用Pushkit静默通知可以实现这一点。

一旦收到Push kit负载,您就必须安排本地通知。

您可以使用“接受/拒绝乘车请求”使本地通知具有交互性,并在触摸事件上执行适当的操作,例如调用API、SQLite等。

请参考以下代码。我按VOIP呼叫功能起草了此草案,您可以根据自己的要求起草。

func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {
        // Process the received push

        var arrTemp = [NSObject : AnyObject]()
        arrTemp = payload.dictionaryPayload

        let dict : Dictionary <String, AnyObject> = arrTemp["aps"] as! Dictionary<String, AnyObject>


        if "IfUserHasLoggedInWithApp" // Check this flag then only proceed
        {                

            if UIApplication.sharedApplication().applicationState == UIApplicationState.Background || UIApplication.sharedApplication().applicationState == UIApplicationState.Inactive
            {

                if "CheckForIncomingCall" // Check this flag to know incoming call or something else
                {

                    var strTitle : String = dict["alertTitle"] as? String ?? ""
                    let strBody : String = dict["alertBody"] as? String ?? ""
                    strTitle = strTitle + "\n" + strBody

                    let notificationIncomingCall = UILocalNotification()

                    notificationIncomingCall.fireDate = NSDate(timeIntervalSinceNow: 1)
                    notificationIncomingCall.alertBody =  strTitle
                    notificationIncomingCall.alertAction = "Open"
                    notificationIncomingCall.soundName = "SoundFile.mp3"
                    notificationIncomingCall.category = dict["category"] as? String ?? ""

                    notificationIncomingCall.userInfo = "As per payload you receive"

                    UIApplication.sharedApplication().scheduleLocalNotification(notificationIncomingCall)

                    }
                    else
                    {
                        //  something else
                    }

        }
    }

}

参考更多关于推送工具集成和负载的内容。
请查看以下链接:https://github.com/hasyapanchasara/PushKit_SilentPushNotification 此外,以下图片可能对您有帮助:enter image description here

1
Hasya,谢谢。我们如何为本地通知显示自定义UI?通知内容扩展? - jkdev
1
我们可以在同一个应用程序中同时使用普通推送通知和VoIP推送吗?我的意思是说我们需要将它们分别处理吗? - jkdev
1
非常感谢!关于正常推送和VoIP,我们应该在后端保持单独的配置吗?我的后端已经配置了Amazon SNS。现在我们有普通APNS令牌用于正常推送和VoIP令牌。我们可以使用VoIP令牌进行正常推送吗?提前致谢。 - jkdev
是的,推送通知令牌和VoIP推送令牌是不同的。因此,您需要在后端分别进行更改。 - Hasya
1
谢谢你的快速回复。你的意思是我们需要保留处理两个令牌吗? - jkdev
显示剩余12条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接