使用 Pub/Sub 模型在 Ionic 2 中推送通知 [英] Push Notifications in Ionic 2 with the Pub/Sub Model

查看:15
本文介绍了使用 Pub/Sub 模型在 Ionic 2 中推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 phonegap-plugin-push 插件在Ionic 2 聊天应用程序.

I am using the phonegap-plugin-push plugin to do Push Notifications in an Ionic 2 Chat App.

我已经根据 文档,它可以工作,但只能在同一设备上.即,如果您提供其 device token,它会向手机发送通知.

I've implemented it according to the documentation, and it works, but only on the same device. i.e. it sends a notification to the phone if you supply its device token.

我的问题是关于一个聊天应用,我需要一个 PubSub 模型.这样一个用户可以发布到一个主题,而另一个用户可以订阅该主题,即使他们在不同的设备上.

My issue is for a chat app, I need a PubSub model. So that a user can publish to a topic and another user can subscribe to that topic even if they are on different decices.

查看 文档,它似乎是可能的.请参阅 android.topicsios.topics.

Looking at the documentation, it seems to be possible. See android.topics and ios.topics.

android.topics 数组 [] 可选.如果数组包含一个或多个strings 每个字符串将用于订阅一个 GcmPubSub 主题.

android.topics array [] Optional. If the array contains one or more strings each string will be used to subscribe to a GcmPubSub topic.

所以我尝试以下方法:

Javascript 客户端:

Javascript Client:

发布

    let push = Push.init({
      android: {
        senderID: "xxxxxxxx",
        topics: ['foo']
      },
      ios: {
        alert: "true",
        badge: false,
        sound: "true",
        topics: ['foo']
      },
      windows: {}
    });

push.on('registration', (data) => {
  // call the Java Server
  let promise: Promise<string> = this.notificationService.push(data.registrationId, 'This is a test message from Ionic Chat');
  promise.then((data) => {

  });
});

Java 服务器:

try {
    System.out.println("NotificationService: device_token: "+device_token);
    logger.info("NotificationService: device_token: "+device_token);
    // Prepare JSON containing the GCM message content. What to send and where to send.
    JSONObject jGcmData = new JSONObject();
    JSONObject jData = new JSONObject();
    jData.put("title", "This is title");
    jData.put("message", message);
    jGcmData.put("to", device_token);  // <===== is this the problem???? it is the data.registrationId from the client
    // What to send in GCM message.
    jGcmData.put("data", jData);
    // Create connection to send GCM Message request.
    URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "key=" + API_KEY);
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    // Send GCM message content.
    OutputStream outputStream = conn.getOutputStream();
    outputStream.write(jGcmData.toString().getBytes());
} catch (Exception e) {
    e.printStackTrace();
}

Javascript 客户端:

Javascript Client:

订阅

    let push = Push.init({
      android: {
        senderID: "xxxxxxxx",
        topics: ['foo']
      },
      ios: {
        alert: "true",
        badge: false,
        sound: "true",
        topics: ['foo']
      },
      windows: {}
    });

    push.on('notification', (data) => {

    });

它在同一设备上工作,但如果 publishersubscriber 在不同的设备上,它就不起作用.

It works on the same device, but it doesn't work if the publisher and subscriber are on different devices.

我认为问题可能是即使另一个用户订阅了我发布到的主题,它仍然只发布到 data.registrationId(设备令牌).

I think the problem may be that even though another user is subscribed to the topic I publish to, it is still only publishing to the data.registrationId (Device Token).

请参阅 Java 服务器上的以下行:

See the following line on the Java Server:

jGcmData.put("to", device_token);

问题

有谁知道如何将其发送给该主题的所有订阅者?

Does anyone know how to make it send to all subscribers on that topic?

我认为答案是这里:

"to": "/topics/foo-bar",

但是对于多个主题应该如何格式化呢?(这只是一个主题foo-bar)

But how should this be formatted for multiple topics? (that's just one topic foo-bar)

推荐答案

需要更改Java Server发送到主题而不是设备:

Need to Change the Java Server to send to the topic instead of the device:

jGcmData.put("to", "/topics/"+topics);

请参阅:https://developers.google.com/cloud-messaging/

这篇关于使用 Pub/Sub 模型在 Ionic 2 中推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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