【问题标题】:IOS Notification when charging充电时的IOS通知
【发布时间】:2015-10-16 11:54:01
【问题描述】:

例如,可以从应用程序获取通知:

  • 我插上充电器?
  • 我的电量是 100%?

是否有可能从此通知中获取要完成的操作?

最后一个:即使我的应用程序没有运行,是否有可能收到此通知?就像我收到 Whatsapp 消息时收到推送通知一样?

感谢您的帮助!

【问题讨论】:

    标签: ios background notifications apple-push-notifications


    【解决方案1】:

    您可以使用this code from Apple 监控电池电量和状态。

    float batteryLevel = [UIDevice currentDevice].batteryLevel; //gets the battery's level
    UIDeviceBatteryState currentState = [UIDevice currentDevice].batteryState; //gets the battery's status
    
    // Register for battery level and state change notifications.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(batteryLevelChanged:)
                                                 name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(batteryStateChanged:)
                                                 name:UIDeviceBatteryStateDidChangeNotification object:nil];
    

    您可以使用这些通知来触发您想要的任何事件(例如本地通知)。
    但是,如果您的应用没有运行(无论是在前台还是在后台),您将无法监控任何内容。

    【讨论】:

    • 您好昆汀,非常感谢您的回答。背景:是否可以像 Viber 或 Whatsapp 类型的背景(即使应用程序未启动也会通知消息),还是必须启动应用程序并保持在后台?
    • Whatsapp 之类的通知是推送。它们是从 Whatsapp 的服务器发送的,即使应用程序终止,iOS 也会显示它们。在您的情况下,您需要在设备上执行代码以获取电池状态。如果您的应用被终止,就不会发生这种情况。
    • 好的。我懂了。推送通知仅来自服务器,而不来自 IOS 或设备!这是我目前不明白的。感谢您的帮助!
    • 您有看起来像推送通知的本地通知。但是,虽然您可以安排触发它们,但如果您的应用未运行,您将无法主动显示它们。
    猜你喜欢
    • 2011-07-29
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多