How can I customize a Push-Notification?

耗尽温柔 提交于 2020-03-26 04:02:32

问题


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

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;
                        }
                    });

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)")
}

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>>>

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"
}

But without success.

Furthermore, by using something like:

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

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>>>

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.


回答1:


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

  • 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.

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



来源:https://stackoverflow.com/questions/60484864/how-can-i-customize-a-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!