我如何自定义推送通知?

问题描述 投票:0回答:1

我正在使用iOS应用程序在Parse-Server(Heroku)上进行推式通知工作的项目。

这是我在服务器端用于生成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应用程序方面,我收到了通知,但如果可能,我想根据自己的喜好对其进行调整。

这里是相关的快速代码,在这里我可以看到通知的到来:

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个回答
0
投票

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

  • 警告:该通知的消息。
  • 徽章:(仅适用于iOS)应用程序图标右上角指示的值。可以将其设置为一个值或“增量”,以使当前值增加1。
  • 声音:(仅适用于iOS)应用程序捆绑包中声音文件的名称。
  • 内容可用:(仅适用于iOS),如果您使用iOS7中引入的“远程通知后台模式”(也称为“ Background Push”)编写应用程序,请将此值设置为1以触发后台下载。您还必须从iOS 13和watchOS 6开始设置push_type。
  • push_type :(仅适用于iOS)通知的类型。该值为警报或背景。在通知的传递显示警报,播放声音或标记应用程序图标时指定警报。为不与用户交互的静默通知指定背景。如果未设置任何值,则默认为发出警报。向运行iOS 13或更高版本或watchOS 6或更高版本的设备发送通知时需要。
  • 优先级:(仅适用于iOS)通知的优先级。指定10立即发送通知。指定5以根据用户设备的功耗考虑发送通知。 (更多详细文档)
  • 类别:(仅iOS)此推送通知的UNNotification类别的标识符。
  • uri :(仅适用于Android)包含URI的可选字段。打开通知后,将启动与打开URI相关的活动。
  • 标题:(仅适用于Android)Android系统任务栏通知中显示的值。

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

© www.soinside.com 2019 - 2024. All rights reserved.