简体   繁体   English

静默通知

[英]Silent notifications

In an iOS app using remote notifications (coming from Parse.com) I have the following issue: While it is often appreciated to see a little note telling the user that something new has arrived, there are also times when silence is much more appreciated. 在一个使用远程通知的iOS应用程序中(来自Parse.com),我遇到以下问题:尽管经常会看到一条小提示告诉用户新内容已经到来,但有时人们更喜欢沉默。 Knowing that the app is doing its job as it should (in the background), without being told about every single event is what we want. 我们想要的是知道应用程序正在做其应有的工作(在后台),而无需告知每个事件。

How can I achieve that? 我该如何实现?

Browsing the net I've read about UIBackgroundModes in Info.plist; 浏览网络我已经在Info.plist中阅读了有关UIBackgroundModes的内容; but when I tried I didn't get the result I was hoping for. 但是当我尝试时却没有得到想要的结果。 I guess I must either be using the wrong tool or missusing it. 我想我一定是使用了错误的工具或滥用了它。 I've also read that Apple may be picky about the usage of these UIBackgroundModes when reviewing apps. 我还读到,Apple在审查应用程序时可能会对这些UIBackgroundModes的使用保持挑剔。 So I may as well avoid looking for trouble if possible. 因此,我也尽可能避免麻烦。

To avoid misunderstanding I put the code below, which is working; 为了避免造成误解,我将下面的代码正常工作; except for the fact that it is "noisy". 除了它“嘈杂”。 Meaning the app is happy to receive notifications (and this works); 表示该应用很乐意收到通知(此方法有效); but the user would be happy not to know about it, meaning not to see "ABC" on the phone all the time. 但是用户会很高兴不知道它,这意味着不会一直在电话上看到“ ABC”。

On the cloud: 在云端:

function pushNotification()
{
    var pushQuery = new Parse.Query(Parse.Installation);
    pushQuery.equalTo(‘fieldXZY’, 'ios');

    Parse.Push.send({
                    where: pushQuery,
                    data: {
                    alert: “ABC"
                    }
                    }, {
                    success: function() {},
                    error: function(error) {
                    throw "Got an error " + error.code + " : " + error.message;
                    }
                    });
}

Inside the app: 在应用内:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSString *notifString=[[userInfo valueForKey:@"aps"] valueForKey:@"alert"];

    if ([notifString isEqualToString:@“ABC"]) // The Notification has come.
        [self doTheWorkForABCNotif];
}

Don't send sound key in aps if you want no voice for receiving notification. 如果您不想让语音接收通知,请不要在aps中发送声音键。 aps should be similar as below APS应该类似于以下内容

aps = {
  alert = "You have received a message";
  badge = 3;
  "content-available" : 1,// this key will allow to receive payload data even if app is in background
};

remove alert also if you don't want to show alert . 删除警报此外,如果你不想自动显示警告。 do what ever you want in custom payload keys 在自定义有效负载密钥中执行您想要的任何操作

As far as I know, there is no api to disable the push notification sound in native code. 据我所知,没有API可以禁用本机代码中的推送通知声音。 The user may disable or enable it through Settings->Notifications->your app 用户可以通过设置->通知->您的应用禁用或启用它

However I suppose it is possible to achieve this with custom sound specification. 但是我想可以通过自定义声音规范来实现。 Check this apple doc if you don't know. 如果您不知道,请查看此苹果文档 To do this, put an custom sound file which contains no sounds at all. 为此,放置一个自定义声音文件,其中根本不包含声音。 And then, if you don't want the user to receive the sound, just specify the sound file in the JSON payload before you send it to Apple Push Notification Service 然后,如果您不希望用户接收声音,则只需在JSON有效负载中指定声音文件,然后再将其发送到Apple Push Notification Service即可。

Silent notifications are mighty useful if you want to trigger something in the app (like a background fetch, data clearing in the database, updating sync dates etc.) To successfully achieve this you will need three things 如果您想在应用程序中触发某些操作(例如后台获取,数据库中的数据清除,更新同步日期等),则静默通知非常有用。要成功实现此目的,您需要做三件事

1) Background modes 1)背景模式

In your app's Info.plist , you need to add App downloads content in response to push notifications as one of the Required background modes 在应用程序的Info.plist中,您需要添加应用程序下载内容以响应推送通知 ,这是必需的后台模式之一

2) Delegate callback 2)委托回调

In your app delegate you need to implement 在您的应用程序委托中,您需要实现

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// Do your stuff here 
}

Note the additional 注意其他

fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

block in the callback 阻止回调

and also implement 并实施

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// Run your background fetch here
}

3) Notification Payload 3)通知有效载荷

The payload needs to be in the following structure (in addition to any custom payload) 有效负载必须采用以下结构(除了任何自定义有效负载之外)

 {
        "aps" : {
            "content-available" : 1,
            "sound" : ""
        }
        "customPayloadKey":3,
        "additionalPayload": {
                               "customKey":"customValue"
                             }
    }

Cheers ! 干杯!

If you are using Parse, they have an option to send in message via JSON format. 如果您使用的是Parse,则他们可以选择通过JSON格式发送消息。

For example : {"message":"Hello World","badge":"1","sound":"silent.mp3"} 例如:{“ message”:“ Hello World”,“ badge”:“ 1”,“ sound”:“ silent.mp3”}

You can add an audio file to your app bundle which goes by the name of silent.mp3. 您可以将音频文件添加到您的应用包中,该文件的名称为silent.mp3。 This audio file could be just an empty recorded audio. 该音频文件可能只是一个空的录制音频。

However, this would require you to update your app to the store as you have to add an audio file to your bundle.When you send in the above JSON, it would try to find the audio file named silent.mp3 in your app bundle and play that file as notification sound. 但是,这需要您将应用程序更新到商店,因为您必须将音频文件添加到包中。当您发送上述JSON时,它将尝试在应用程序包中找到名为silent.mp3的音频文件,然后播放该文件作为通知声音。

Since, the audio file is just a silent recording, there won't be any notification sound. 由于音频文件只是静默录音,因此不会有任何通知声音。 You may add various audio files to your bundle and control it via Parse when you send in the Push Notification. 您可以在发送“推送通知”时将各种音频文件添加到包中,并通过“解析”对其进行控制。

I hope this provides you with a workaround solution. 我希望这为您提供了一种解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM