【问题标题】:Why same notification is pushed many times to android and iphone devices为什么相同的通知被多次推送到 android 和 iphone 设备
【发布时间】:2013-11-13 04:55:38
【问题描述】:

我使用 javapns 向 ios 推送通知,使用 android.gcm.server 向 android 设备推送通知。但是,当我向许多设备发送一个通知时,每个设备都会收到多个发送的通知副本。有时这个数字是 2,有时是 3。它几乎不会只提供我一直期望的一个。有什么想法吗?

我的代码如下

public void pushNotificationsToAndroid(String pushMessage,
            String contentType, String content, String notification_id,
            List<String> devices) {



        try {

            Sender sender = new Sender(
                    properties
                            .getProperty("notification.android.senderIdDemo"));
            com.google.android.gcm.server.Message message = new com.google.android.gcm.server.Message.Builder()
                    .collapseKey("1").timeToLive(3).delayWhileIdle(true)
                    .addData("message", pushMessage)
                    .addData("content_type", contentType)
                    .addData("content", content)
                    .addData("notification_id", notification_id).build();

            MulticastResult result = sender.send(message, devices, 1);

            if (result.getResults() == null) {

                System.out.println(result.getFailure());
                logger.debug("getFailure() of sender.send() method :",
                        result.getFailure());

            }
        } catch (Exception exception) {
            logger.error("erorr push notification ");

        }

        System.out.println("sent not at " + new Date());
        logger.debug(
                "exit pushNotificationsToAndroid() method : current time is ",
                new Date());

    }

    public void pushNotificationsToIOS(String pushMessage, String contentType,
            String content, String notification_id, List<String> devices)
             {


        boolean production = true;
        String password = properties
                .getProperty("notification.ios.password");

        String keyStroke = properties
                .getProperty("notification.ios.certFileName");

        AppleNotificationServer jksServer = null;


        try {

            jksServer = new AppleNotificationServerBasicImpl(keyStroke,
                    password, ConnectionToAppleServer.KEYSTORE_TYPE_JKS,
                    production);

        } catch (KeystoreException keystoreException) {

            logger.error("erorr creating jksServer");

        }

        PushNotificationPayload payload = PushNotificationPayload.complex();

        try {

            payload.addAlert(pushMessage);

        } catch (JSONException e2) {

            logger.error("erorr creating payload alert");

        }

        try {

            payload.addCustomDictionary("content_type", contentType);

        } catch (JSONException e1) {

            logger.error("erorr creating payload content_type");

        }

        try {

            payload.addCustomDictionary("content", content);

        } catch (JSONException e1) {

            logger.error("erorr creating payload content");

        }

        try {

            payload.addCustomDictionary("notification_id", notification_id);

        } catch (JSONException e1) {

            logger.error("erorr creating payload notification_id");

        }

        PushNotificationManager pushManager = new PushNotificationManager();

        try {

            pushManager.initializeConnection(jksServer);

        } catch (CommunicationException | KeystoreException e) {

            logger.error("erorr connecting Server");


        }
        try {

            List<PushedNotification> notifications = pushManager
                    .sendNotifications(payload, Devices.asDevices(devices));


        } catch (CommunicationException | KeystoreException e) {

            logger.error("erorr push notifications");

        }

    }

【问题讨论】:

  • 发布你的代码...!!!!!!
  • 问题应该出在您的服务器端代码中...
  • @GauravPandey 我发布了代码

标签: java android ios google-cloud-messaging javapns


【解决方案1】:

android official site 中谈到了重复消息条件的一些原因。

【讨论】:

  • 是的,我看到了。但我的案例问题是一个与线程相关的问题,我解决了这个问题。感谢您的帮助
  • 你能清楚地告诉我你的问题吗?因为我也面临同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多