如何自定义推送通知? [英] How can I customize a Push-Notification?

查看:19
本文介绍了如何自定义推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,我想让推送通知在 Parse-Server (Heroku) 上使用 iOS 应用程序运行.

I am working on a project where I want to make Push notifications work, on Parse-Server (Heroku), with an iOS app.

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

This is the code I use on the server side to generate a 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 应用方面,我收到了通知,但如果可能的话,我想根据自己的喜好对其进行调整.

On the iOS app side, I receive the notification, but I would like to tweak it to my taste, if possible.

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

Here is the relevant swift code where I can see the notification coming:

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

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

And finally, this is what I can see in the Xcode debugging console, when the notification arrives:

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"是我在服务器端设置的.

Obviously, it is working up to a point. But I can see fields in the incoming notification that I do not control. Namely: title, subtitle, summaryArgument, categoryIdentifier ...... In fact only "body" is what I have set on the server side.

因此我的问题是:如何按照我希望的方式设置所有这些字段.

Therefore my question is: how can I set all those fields the way I wish.

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

I have already tried something like this:

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

但没有成功.

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

Furthermore, by using something like:

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

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

I can see the following in the Xcode debugging console, when the notification arrives:

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)似乎被忽略了.

Where it appears that the "ABCXYZ" part is transferred as well as the category (S3x), but the rest (content_available,push_type) seems to be ignored.

推荐答案

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

According to Parse push notification docs, you can send the following options in your push notification payload:

  • alert:通知的消息.
  • 徽章:(仅限 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

Besides these, you can also write a custom push notification handler in your iOS app by following this guide: http://docs.parseplatform.org/ios/guide/#responding-to-the-payload

这篇关于如何自定义推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆