【问题标题】:How can I customize a Push-Notification?如何自定义推送通知?
【发布时间】:2020-06-14 12:20:14
【问题描述】:

我正在开发一个项目,我希望在 Parse-Server (Heroku) 上使用 iOS 应用程序来实现推送通知。

这是我在服务器端用来生成 PUSH 的代码:

const pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo('deviceType', 'ios');
    Parse.Push.send({
                        where: pushQuery, // Set our Installation query
                        data: {alert: "ABCXYZ"}
                    }, {
                        useMasterKey: true,
                        success: function() {},
                        error: function(error) {
                            throw "Got an error " + error.code + " : " + error.message;
                        }
                    });

在 iOS 应用端,我收到了通知,但如果可能的话,我想根据自己的喜好对其进行调整。

这是相关的 swift 代码,我可以在其中看到通知:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print(#function)
    print("NN = \(notification.description)")
}

最后,当通知到达时,我可以在 Xcode 调试控制台中看到:

userNotificationCenter(_:willPresent:withCompletionHandler:)
NN = <UNNotification: 0x2....; date: 2020-03-02 06:51:39 +0000,
request: <UNNotificationRequest: 0x28....3e0; identifier: 0C....AF3,
content: <UNNotificationContent: 0x28....6c0; title: (null), subtitle: (null),
body: ABCXYZ, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: ,
launchImageName: , threadIdentifier: , attachments: (
), badge: (null), sound: (null),, 
trigger: <UNPushNotificationTrigger: 0x28...0; 
contentAvailable: NO, mutableContent: NO>>>

显然,它正在发挥作用。但我可以在传入通知中看到我无法控制的字段。即:title、subtitle、summaryArgument、categoryIdentifier……其实只有“body”是我在服务器端设置的。

因此我的问题是:如何按照自己的方式设置所有这些字段。

我已经尝试过这样的事情:

data: {
  title: "MyOwnTitle",
  alert: "ABCXYZ"
}

但没有成功。

此外,通过使用类似的东西:

data: {
  alert: "ABCXYZ",
  content_available: 1,
  push_type: "background",
  category: "S3x"
}

当通知到达时,我可以在 Xcode 调试控制台中看到以下内容:

userNotificationCenter(_:willPresent:withCompletionHandler:)
NN = <UNNotification: 0x28...; date: 2020-03-03 ...,
request: <UNNotificationRequest: 0x2..;
identifier: BF...EE, content: <UNNotificationContent: 0x28..;
title: (null), subtitle: (null), body: ABCXYZ,
summaryArgument: , summaryArgumentCount: 0,
categoryIdentifier: S3x, launchImageName: ,
threadIdentifier: , attachments: (
), badge: (null), sound: (null),, 
trigger: <UNPushNotificationTrigger: 0x28..; 
contentAvailable: NO, mutableContent: NO>>>

似乎“ABCXYZ”部分和类别 (S3x) 一样被转移,但其余部分 (content_available,push_type) 似乎被忽略了。

【问题讨论】:

    标签: swift apple-push-notifications parse-server payload heroku-api


    【解决方案1】:

    根据 Parse 推送通知docs,您可以在推送通知负载中发送以下选项:

    • alert:通知消息。
    • badge:(仅限 iOS)应用图标右上角指示的值。这可以设置为一个值或 Increment,以便将当前值增加 1。
    • sound:(仅限 iOS)应用程序包中声音文件的名称。
    • content-available:(仅限 iOS)如果您正在使用 iOS7 中引入的远程通知后台模式(也称为“后台推送”)编写应用程序,请将此值设置为 1 以触发后台下载。您还必须从 iOS 13 和 watchOS 6 开始设置 push_type。
    • push_type:(仅限 iOS)通知的类型。该值为警报或背景。当您的通知发送显示警报、播放声音或标记您的应用程序图标时,请指定警报。为不与用户交互的静默通知指定背景。如果未设置任何值,则默认为警报。向运行 iOS 13 及更高版本或 watchOS 6 及更高版本的设备发送通知时需要。
    • priority:(仅限 iOS)通知的优先级。指定 10 以立即发送通知。指定 5 以根据用户设备上的电源考虑发送通知。 (更详细的文档)
    • category:(仅限 iOS)此推送通知的 UNNotification​Category 的标识符。
    • uri:(仅限 Android)包含 URI 的可选字段。打开通知时,将启动与打开 URI 关联的 Activity。
    • title:(仅限 Android)Android 系统托盘通知中显示的值。

    除此之外,您还可以按照以下指南在您的 iOS 应用中编写自定义推送通知处理程序:http://docs.parseplatform.org/ios/guide/#responding-to-the-payload

    【讨论】:

    • 我尝试使用您提到的文档来利用您的回答中的建议。但是仍然不完全清楚使用什么语法。我在帖子中添加了一些内容以反映我的尝试。
    • 试试content-availablepush_type 我不确定你是否能在调试中看到最后一个
    • 因为我不希望用户被某些通知所困扰。它尝试了:“push_type:“背景”,”。但它似乎根本没有任何效果。我们仍然可以看到通知即将到来。
    • 至少我取得了一些进展,基本上得到了我需要的东西,除了我刚才提到的一些细节。还有我仍然不知道如何使用的“content_available”。顺便说一句,我阅读的文档提到了“内容可用”,但这会产生错误。使用“content_available”不会产生错误,但没有任何好处。
    • 您是否尝试过按照docs.parseplatform.org/ios/guide/#responding-to-the-payload 中的说明构建自定义处理程序?
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多