【问题标题】:implement Push Notifications in Ionic 2 with the Pub/Sub Model使用 Pub/Sub 模型在 Ionic 2 中实现推送通知
【发布时间】:2017-04-03 16:25:33
【问题描述】:

您好,这是问题的重复

Push Notifications in Ionic 2 with the Pub/Sub Model

我已经在这篇文章之后实现了推送通知> https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59#.xvoeao59a

我想要的是能够在应用程序中发生某些事件(例如聊天或预订或新职位发布)时向用户发送通知。

如何走得更远,这是我的第一个应用程序。

【问题讨论】:

  • 您的 ionic 应用程序是否已链接到某种后端(例如 Java REST API)?
  • 不,我使用 firebase 作为数据库和文件存储
  • @Ivaro18 服务器设置代码需要后端吗?
  • 不知道,但我就是这样做的,所以现在帮不了你:D
  • 我正在学习做,如果你能告诉如何继续可以学习:D

标签: ionic-framework push-notification cordova-plugins ionic2


【解决方案1】:

注意:代码与教程几乎一模一样,Java 只是转换为 Kotlin

这是我的实际离子侧代码(在登录页面上)。 push.on('registration') 将在用户打开应用程序时触发,变量 this.device_id 稍后将(在成功登录时)发送到我的 Kotlin REST API,因此我知道 device_id 并将其耦合到用户。这样您就可以发送有针对性的推送通知。

如果您从 Kotlin 发送推送通知(代码如下所示,看起来有点像 Java),与 Google 的连接(始终打开,甚至在启动后打开)将向您的设备(由 device_id 定义)发送一条消息通知数据(标题、消息等),之后您的设备将识别 senderID 并将其匹配以使用您的 ionic 应用程序。

initializeApp() {
    this.platform.ready().then(() => {

      let push = Push.init({
        android: {
          senderID: "1234567890"
        },
        ios: {
          alert: "true",
          badge: false,
          sound: "true"
        },
        windows: {}
      });

      //TODO - after login
      push.on('registration', (data) => {
        this.device_id = data.registrationId;
      }); 

      push.on('notification', (data) => {
        console.log('message', data.message);
        let self = this;
        //if user using app and push notification comes
        if (data.additionalData.foreground) {
          // if application open, show popup
          let confirmAlert = this.alertCtrl.create({
            title: data.title,
            message: data.message,
            buttons: [{
              text: 'Negeer',
              role: 'cancel'
            }, {
              text: 'Bekijk',
              handler: () => {
                //TODO: Your logic here
                this.navCtrl.setRoot(EventsPage, {message: data.message});
              }
            }]
          });
          confirmAlert.present();
        } else {
          //if user NOT using app and push notification comes
          //TODO: Your logic on click of push notification directly
          this.navCtrl.setRoot(EventsPage, {message: data.message});
          console.log("Push notification clicked");
        }
      });
      push.on('error', (e) => {
        console.log(e.message);
      });

    });
  }

Kotlin 代码(由 Java 示例转换而来,基本相同

package mycompany.rest.controller

import mycompany.rest.domain.User
import java.io.OutputStream
import java.net.HttpURLConnection
import java.net.URL

class PushNotification {    
    companion object {
        val SERVER_KEY = "sOmE_w31rD_F1r3Ba5E-KEy";

        @JvmStatic fun sendPush(user: User, message: String, title: String) {
            if(user.deviceId != "unknown"){
                val pushMessage = "{\"data\":{\"title\":\"" +
                title +
                "\",\"message\":\"" +
                message +
                "\"},\"to\":\"" +
                user.deviceId +
                "\"}";


                val url: URL = URL("https://fcm.googleapis.com/fcm/send")
                val conn: HttpURLConnection = url.openConnection() as HttpURLConnection
                conn.setRequestProperty("Authorization", "key=" + SERVER_KEY)
                conn.setRequestProperty("Content-Type", "application/json")
                conn.setRequestMethod("POST")
                conn.setDoOutput(true)

                //send the message content
                val outputStream: OutputStream = conn.getOutputStream()
                outputStream.write(pushMessage.toByteArray())
                println(conn.responseCode)
                println(conn.responseMessage)
            }else {
                println("Nope, not executed")
            }
        }   

        @JvmStatic fun sendPush(users: List<User>, message: String, title: String) {
            for(u in users) {
                PushNotification.sendPush(u, message, title)
            }
        }
    }
}

那么该方法可以调用为PushNotification.sendPush(user1, "Hello world!", "my title");

(顺便说一句,您不需要从服务器(本地主机/外部)运行推送通知。您可以创建一个主类,使用硬编码的deviceId 发送它以进行测试。

【讨论】:

  • 喜欢你 m3nt10n3d k3y 的方式,让我试试代码
  • 很抱歉,后来的依赖被其他离子的东西卡住了,最后我能够使用 rc3 进行推送通知......我正在从 localhost 运行 java 代码,我想知道的是如何调用 java 服务器 push.on('registration', (data) => { // 调用 Java 服务器 ??????? 怎么做这个 bit let promise: Promise = this.notificationService. push(data.registrationId, '这是来自 Ionic Chat 的测试消息'); promise.then((data) => { }); });
  • 一个解决方案是查看 Spring,然后您可以在 java (fe localhost:8080/push ) 中定义一个带有您的标题和消息的 url,在该 url 调用的函数中发送推送通知从 ionic 你可以用this.http.post(url, data, headers?) 调用它
  • 我比以往任何时候都更迷茫,你是如何发送和存储设备 ID 的? " this.device_id 稍后将(在成功登录时)被发送到我的 Kotlin REST API,所以我知道 device_id 并将其耦合到用户。"
  • 我自己将其发送到 Header 对象中,这将与您的数据一起发送,您可以在 API 中以 @RequestHeader(value="device_id") 的形式检索它
猜你喜欢
  • 1970-01-01
  • 2017-01-27
  • 2014-08-25
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
相关资源
最近更新 更多