【问题标题】:Send push notifications to Android向 Android 发送推送通知
【发布时间】:2016-05-09 02:42:24
【问题描述】:

我目前正在开发在服务器上的 WebLogic 上运行的 Java Web 服务。这些 Web 服务由另一个团队开发的移动应用程序调用。我需要向移动应用程序发送推送通知。我目前的开发不需要我做任何移动开发,因为我只做服务器端开发。我也没有移动开发经验。如何为两个 Android 设备开发推送通知?

谢谢。

【问题讨论】:

  • 您有 GCM 服务设置吗?
  • 不,还没有。我实际上不了解 GCM。我们可以使用 GCM 发送到 Android 和 iPhone 设备吗?
  • 我不这么认为。为此,您需要一个 APN。一个是谷歌,另一个是苹果。遗憾的是,推送通知服务并不是一个明确定义的标准
  • 如果你想管理自己的推送通知服务,那么Parse Server has documentation,否则可以类似地设置AWS来发送推送通知
  • 我不明白您所说的“管理您自己的推送通知服务”是什么意思。 Parse Server 与 GCM 和 APNS 有何不同?

标签: android push-notification google-cloud-messaging android-notifications


【解决方案1】:

GCM 应用的先决条件

  1. Google API 服务器密钥
  2. 要通过 GCM 进行通信的 Android 设备的 GCM RegId

如果您对GCM有清晰的概念,请访问here

您的托管服务器不需要任何 Ip/HostName 来发送消息,因为此消息将通过 com.google.android.gcm.server.Sender 此类进行审议。此类将在内部与 GCM 服务器通信。 您正在使用这种方式配置此类:

Sender sender = new Sender(API_KEY);

您可以使用 GCM 服务器发送消息。此代码将用于向设备发送推送通知。这可以从 java 服务器向任何 Android/IOS 应用程序发送通知。

Here 是这个 GCM 服务器库的 jar。

这里,这个构造函数获取 MESSAGE 和 deviceGcmId 必须发送推送消息。

public PushNotification(String id, String message) {
            this.receiverID = id;
            this.gcmMessage = message;
        }

这里是示例代码:

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

public class PushNotification {
    private String receiverID;
    private String gcmMessage;
    String MESSAGE_KEY = "YOUR_MESSAGE_KEY";
    String API_KEY = "YOUR_API_KEY";

    Result result;
    Sender sender;
    Message message;
    MulticastResult multicastResult;

    public PushNotification() {
    }

    public PushNotification(String id, String message) {
        this.receiverID = id;
        this.gcmMessage = message;
    }

    public boolean sendSinglePushNotification() {
        try {
            sender = new Sender(API_KEY);
            message = new Message.Builder().timeToLive(30).addData(MESSAGE_KEY, gcmMessage).build();
            result = sender.send(message, receiverID, 1);
            if (result != null && result.getErrorCodeName() == null) {
                return true;
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
        return false;
    }
}

你可以这样调用这个类:

PushNotification notification=new PushNotification("DEVICE_GCM_ID","MESSAGE HAVE To SEND");    
notification.sendSinglePushNotification();

就是这样:)

【讨论】:

  • 您好,两个问题:(1)您的示例代码仅适用于Android?据说GCM现在同时支持Android和iOS,那么你的代码是适用于两者还是只适用于Android? (2) 您的代码未指定任何目标服务器主机名/IP 或 Google GCM 服务器的端口。当我们通过 GCM 发送通知时,GCM API 是如何知道如何与 Google GCM Server 通信的?
  • 您好,请参阅我编辑的答案。这里的服务器端代码不是客户端。使用此代码,您无需知道谁是客户端,例如,anroid 或 ios。您只需要客户端 GcmId。我添加了一个链接,您可以在其中获取有关 GCM 服务器、客户端通信机制的描述。谢谢
  • 您好,您的代码未指定 Google GCM 服务器的任何主机名/IP。它如何知道如何与 Google GCM 服务器通信?
  • 您的托管服务器不需要任何 Ip/HostName 来发送消息,因为此消息将通过 com.google.android.gcm.server.Sender 此类进行审议。此类将在内部与 GCM 服务器通信。
  • 您好,现在有FCM,它是GCM的升级版。 FCM 的示例代码是否仍然相同?我在哪里可以下载 FCM 的 jar?谢谢。
【解决方案2】:

如果您想在多个平台上发送推送通知,请使用 Parse.com 但这里的问题是 Parse.com 将在 2017 年 1 月 28 日停止其服务。你有像 urban airship 这样的替代品等等。我个人推荐城市飞艇。

【讨论】:

    【解决方案3】:

    您可以尝试从 Jersey 发送 Sever Sent Event(SSE),看看是否满足您的要求。

    【讨论】:

    • 嗨,这似乎不是推送通知。
    【解决方案4】:

    您可以使用 Google Cloud Messaging 来执行此操作,它现在支持 Android 和 iOS。它是免费的,并且没有通知数量的限制。

    它是如何工作的?

    简单地说,您需要从您的手机生成注册 ID 并将其发送到您的服务器,然后当您需要发送通知时,只需将注册 ID 和消息发送到 GCM。

    请查看来自 Google 的有关 GCM 的链接:

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

    这个链接只是快速浏览一下服务器代码(用 PHP 编写):

    http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

    您可以在第一个链接中注意到一些变化。

    最后,查看此链接,它逐步解释了客户端代码:

    http://www.androidwarriors.com/2015/10/push-notification-using-gcm-in-android.html

    希望对您有所帮助,有任何问题都不要犹豫。

    【讨论】:

    • 您好,(1) 注册 ID 是否仅适用于 Android? (2) Android 和 iOS 发送通知的代码是一样的吗?
    • @user3573403 (1) 否,需要注册 ID 并且适用于 Android 和 iOS,因为注册 ID 是连接您的设备与 GCM 的唯一方式。 (2) 我认为您需要使用不同的代码,请检查此 PHP 脚本以向 Android 和 iOS 发送推送通知:gist.github.com/joashp/b2f6c7e24127f2798eb2
    【解决方案5】:

    向 Android 发送推送通知

     private void pushNotification(String handlename) {
            StringEntity entity = null;
            JSONObject jsonObject = new JSONObject();
            String referesedtoken = FirebaseInstanceId.getInstance().getToken();
            try {
                jsonObject.put("to", clientToken);
                jsonObject.put("priority", "high");
                JSONArray jsonArray = new JSONArray();
                jsonArray.put(referesedtoken);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("body", handlename);
                jsonObject1.put("commonContact", commonContact);
                jsonObject1.put("profileType", profileType);
                jsonObject1.put("notification-type", "chat");
                jsonObject1.put("target", handlename);
                jsonObject1.put("email", authPreferences.getEmailAddress());
                jsonObject1.put("UniqueId", authPreferences.getUserUid());
                jsonObject1.put("profilePicUrl", authPreferences.getMyProfilePicImageUrl());
                jsonObject1.put("title", profileBeenClasss.getFirstname() + " " + profileBeenClasss.getLastname() + " has sent message to you");
                jsonObject.put("registrationIDs", jsonArray);
                jsonObject.put("notification", jsonObject1);
                entity = new StringEntity(jsonObject.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
            client.setTimeout(80000);
            client.addHeader("Authorization", "key=AIzaSyCxKiM-LlNHfUOZ3QiiZfSGTo3vTAZdAAI");
            client.post(this, "https://fcm.googleapis.com/fcm/send", entity, "application/json", new TextHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, String res) {
                            System.out.println("Send Notification sucessfully" + res.toString());
                       }
    
                        @Override
                        public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
                            System.out.println("notification fail", res.toString());
                        }
    
                    }
            );
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多