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

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

问题描述

我正在一个项目上,我想通过iOS应用在Parse-Server(Heroku)上使推送通知正常工作.

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.

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

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.

推荐答案

根据解析"推送通知

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

  • 警告:通知的消息.
  • 徽章:(仅限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系统任务栏通知中显示的值.
  • alert: the notification’s message.
  • badge: (iOS only) the value indicated in the top right corner of the app icon. This can be set to a value or to Increment in order to increment the current value by 1.
  • sound: (iOS only) the name of a sound file in the application bundle.
  • content-available: (iOS only) If you are a writing an app using the Remote Notification Background Mode introduced in iOS7 (a.k.a. "Background Push"), set this value to 1 to trigger a background download. You also have to set push_type starting iOS 13 and watchOS 6.
  • push_type: (iOS only) The type of the notification. The value is alert or background. Specify alert when the delivery of your notification displays an alert, plays a sound, or badges your app’s icon. Specify background for silent notifications that do not interact with the user. Defaults to alert if no value is set. Required when delivering notifications to devices running iOS 13 and later, or watchOS 6 and later.
  • priority: (iOS only) The priority of the notification. Specify 10 to send the notification immediately. Specify 5 to send the notification based on power considerations on the user’s device. (More detailed documentation)
  • category: (iOS only) the identifier of the UNNotification​Category for this push notification.
  • uri: (Android only) an optional field that contains a URI. When the notification is opened, an Activity associated with opening the URI is launched.
  • title: (Android only) the value displayed in the Android system tray notification.

除此之外,您还可以按照以下指南在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天全站免登陆