php 极光推送 自定义消息,Swift - JPush极光推送的使用7(发送自定义消息)

news/2024/7/24 7:48:52 标签: php 极光推送 自定义消息

我之前的一系列文章讲的都是发送通知的相关内容。JPush极光推送除了可以推送通知,还可以用来发送自定义消息。本文接着介绍下后者。

1,发送自定义消息与发送通知的异同

(1)客户端App只有在运行的时候才能收到自定义消息。而通知不同,不管客户端是否在运行都是能够收到推送过来的通知。

(2)发送自定义消息的话不需要通过 APNs,但相较于通知,可以发送更多的内容(当然还是有长度限制的)。

(3)虽然App退出后就没法收到自定义消息,但 JPush 服务器这时会将其保存成离线消息(具体保留时长可以设置)。当App启动后,会自动获取到这条离线消息。

(4)由于发送自定义消息不需要通过 APNs,且客户端在运行时才能接收。所以比较适合用在在线聊天室、即时通讯等相关应用上。

(5)同发送通知一样,发送自定义消息也可以根据别名、标签发送给指定用户,或者广播的形式发给所有人。

(6)自定义消息同样可以定时发送。

2,自定义消息的样例

(1)服务端页面上填写消息内容,点击“发送”。即可将自定义消息发送到客户端。

925fe14bc19674edafa2d19601dff434.png

4445ae881a7272ce8493fc938586402c.png

(2)客户端如果是运行状态,则会接收到并进行下一步处理(这里直接将收到的自定义消息弹出显示。)如果客户端此刻没有在运行,等客户端下次运行时也会收到之前发送的这条信息。

2d63453feabe7a17acd98c92df821b41.png

3,样例代码

(1)客户端代码:AppDelegate.swift

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication,

didFinishLaunchingWithOptions

launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

//通知类型(这里将声音、消息、提醒角标都给加上)

let userSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound],

categories: nil)

if ((UIDevice.current.systemVersion as NSString).floatValue >= 8.0) {

//可以添加自定义categories

JPUSHService.register(forRemoteNotificationTypes: userSettings.types.rawValue,

categories: nil)

}

else {

//categories 必须为nil

JPUSHService.register(forRemoteNotificationTypes: userSettings.types.rawValue,

categories: nil)

}

// 启动JPushSDK

JPUSHService.setup(withOption: nil, appKey: "7b528331738ec719195798fd",

channel: "Publish Channel", apsForProduction: false)

//监听自定义消息的接收

let defaultCenter = NotificationCenter.default

defaultCenter.addObserver(self, selector: #selector(networkDidReceiveMessage(notification:)),

name:Notification.Name.jpfNetworkDidReceiveMessage, object: nil)

return true

}

//收到自定义消息

func networkDidReceiveMessage(notification:Notification){

var userInfo = notification.userInfo!

//获取推送内容

let content = userInfo["content"] as! String

//获取服务端传递的Extras附加字段,key是自己定义的

//let extras = userInfo["extras"] as! NSDictionary

//let value1 = extras["key1"] as! String

//显示获取到的数据

let alertController = UIAlertController(title: "收到自定义消息",

message: content,

preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "确定", style: .cancel, handler: nil))

self.window?.rootViewController!.present(alertController, animated: true, completion: nil)

}

func application(_ application: UIApplication,

didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

//注册 DeviceToken

JPUSHService.registerDeviceToken(deviceToken)

}

func application(_ application: UIApplication,

didReceiveRemoteNotification userInfo: [AnyHashable : Any],

fetchCompletionHandler

completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

//增加IOS 7的支持

JPUSHService.handleRemoteNotification(userInfo)

completionHandler(UIBackgroundFetchResult.newData)

}

func application(_ application: UIApplication,

didFailToRegisterForRemoteNotificationsWithError error: Error) {

//可选

NSLog("did Fail To Register For Remote Notifications With Error: \(error)")

}

//...................

(2)服务端代码:index.php

//引入代码

require 'JPush/autoload.php';

use JPush\Client as JPush;

if(isset($_POST["message"])){

//初始化

$app_key = "7b528367738ec346395798fd";

$master_secret = "32da4a2c06dc7b24472c9828";

$client = new JPush($app_key, $master_secret);

//简单的消息发送样例

$result = $client->push()

->setPlatform('ios', 'android')

->addAllAudience()

->message($_POST["message"])

->options(array(

"apns_production" => true //true表示发送到生产环境(默认值),false为开发环境

))

->send();

echo 'Result=' . json_encode($result);

}

?>

消息:

发送

4,更完整的发送样例

同推送通知一样,发送自定义消息也可以添加附加字段,以及定时发送。

// 发送自定义消息

$payload = $client->push()

->setPlatform('ios', 'android')

->addAllAudience()

->message($_POST["message"], [

'title' => 'Hello',

'content_type' => 'text',

'extras' => [

'key1' => 'value1'

]

])

->options(array(

"apns_production" => true //true表示发送到生产环境(默认值),false为开发环境

))

->build();

// 创建在指定时间点触发的定时任务

$response = $client->schedule()->createSingleSchedule("指定时间点的定时任务",

$payload, array("time"=>"2016-11-01 15:00:00"));

echo 'Result=' . json_encode($response);


http://www.niftyadmin.cn/n/1866793.html

相关文章

php 匹配度查询sql,php – 在最佳匹配上排序SQL查询

我得到了一个从我的查询返回的结果列表,我想在最佳匹配上订购.我会尽量保持清醒,但如果事情不够清楚,请告诉我,我会尽力使其更清楚.用户已经输入了一个名为findsettings的设置列表.通过这些发现,我正在寻找产品.这一切顺利,直到他应该找出最佳匹配.有几个字段,如min_review,max…

java要实现多次会话,Java第四十二天,会话内容(三),Session(一)

一、概念服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中二、使用1.获取对象HttpServletRequest request ......HttpSession session HttpServletRequest.getSession()2.常用方法Object obj Session.getAttribute(Str…

java局部变量是否会向上转型,java里的一些特别值得注意的地方

return 语句的作用:1.返回值 2.结束某个方法的运行;局部变量必须要初始化,成员变量系统会默认初始值;栈:自动分配连续的空间,后进先出,放置局部变量堆:不连续,放置new出来…

vue项目使用oidc-client

1.安装oidc-client npm install --save vuex npm install oidc-client 2.新建oidc-client.js import Oidc from "oidc-client";export const mgr new Oidc.UserManager({authority: "authority",//认证服务器client_id: "client_id", //表示…

matlab做信号发生,声卡和Matlab的虚拟信号发生器的工作原理

声卡从话筒中获取声音模拟信号,通过模数转换器(ADC),将声波振幅信号采样转换成一串数字信号,存储到计算机中。重放时,这些数字信号送到数模转换器(DAC),以同样的采样速度还原为模拟波形,放大后送到扬声器发…

vue项目列表跳转详情再返回列表还在原位置

keep-alive 是 Vue 内置的一个组件&#xff0c;可以使被包含的组件保留状态&#xff0c;或避免重新渲染 。也就是所谓的组件缓存 1.app.vue <template><div id"app"><!-- <router-view/> --><!-- 可以被缓存的视图组件 --><keep-…

vue项目中使用可编辑div contenteditable

<div ref"InputContainer" contenteditable"true" spellcheck"false" placeholder"文字"></div> spellcheck 属性规定是否对元素进行拼写和语法检查 css设置placeholder文字的颜色 *[contenteditable"true"]:…

vue的H5项目中使用@scroll滚动加载数据

<div ref"list" scroll"handleScroll"></div> created() created() { //判断是手机端和pc端 this.device() if(this.isPc true){ this.clientHeight document.documentElement.clientHeight; window.addEventListener(scroll, this.handleSc…