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

问题描述

我正在使用插件来执行推送通知Ionic 2 Chat App。



我已根据,它可以工作,但只能在同一台设备上运行。即如果你提供设备令牌,它会向手机发送通知。



我的问题是聊天应用,我需要一个 PubSub 模型。这样用户就可以发布主题,而另一个用户可以订阅该主题,即使它们处于不同的决定。



查看,似乎有可能。参见 android.topics ios.topics

所以我尝试以下内容:



Javascript客户:



发布

  let push = Push.init({
android:{
senderID:xxxxxxxx,
主题:[ 'foo']
},
ios:{
alert:true,
badge:false,
声音:true,
主题:['foo']
},
windows:{}
});

push.on('registration',(data)=> {
//调用Java Server
让我们承诺:Promise< string> = this.notificationService.push (data.registrationId,'这是来自Ionic Chat的测试消息');
promise.then((data)=> {

});
});

Java服务器:

 尝试{
System.out.println(NotificationService:device_token:+ device_token);
logger.info(NotificationService:device_token:+ device_token);
//准备包含GCM消息内容的JSON。发送什么以及发送到何处。
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
jData.put(title,This is title);
jData.put(message,message);
jGcmData.put(to,device_token); //< =====这是问题????它是来自客户端的data.registrationId
//在GCM消息中发送什么。
jGcmData.put(data,jData);
//创建连接以发送GCM消息请求。
网址url =新网址(https://gcm-http.googleapis.com/gcm/send);
HttpURLConnection conn =(HttpURLConnection)url.openConnection();
conn.setRequestProperty(授权,key =+ API_KEY);
conn.setRequestProperty(Content-Type,application / json);
conn.setRequestMethod(POST);
conn.setDoOutput(true);
//发送GCM消息内容。
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString()。getBytes());
} catch(例外e){
e.printStackTrace();
}

Javascript客户:



订阅

  let push = Push.init({
android:{
senderID:xxxxxxxx,
主题:['foo']
},
ios:{
alert:true,
badge:false ,
声音:真实,
主题:['foo']
},
windows:{}
});

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

});

它适用于同一台设备,但如果则无效发布者订阅者在不同的设备上。



我认为问题可能是即使其他用户订阅了我发布的主题,它仍然只发布到数据。 registrationId (设备令牌)。



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

  jGcmData.put(to,device_token); 

问题





我认为答案是:

 to:/ topics / foo-bar,

但是如何为多个主题设置格式? (这只是一个主题 foo-bar

解决方案

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

  jGcmData.put(to,/ topics / +主题); 

参见:


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

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.

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.

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

So I try the following:

Javascript Client:

publish

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

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

Subscribe

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

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

    });

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

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

See the following line on the Java Server:

jGcmData.put("to", device_token);

Question

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

I think the answer is here:

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

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

解决方案

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

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

see: https://developers.google.com/cloud-messaging/

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

08-28 22:54