简体   繁体   中英

How to get registration id of failure push notification on android device using Java GCM API

I have implemented below code in java using GCM API to send notification on Android device.

    Sender sender = new Sender(GCM_API_KEY);
    Message.Builder builder = new Message.Builder();
    builder.collapseKey(collpaseKey);
    builder.timeToLive(TTL_HOURS * 3600);
    if (data != null) {
        for (PushData messageData : data) {
            builder.addData(messageData.getKey(), messageData.getValue());
        }
    }
    if (StreamType.CALL == type) {
        for (PushData pushData : message) {
            builder.addData(pushData.getKey(), pushData.getValue());
        }
    }
    Message msg = builder.build();
    try {
        System.out.println(msg.toString());
        MulticastResult result = sender.send(msg, pushId, MAX_RETRY);
        log.infof("result %s", result.toString());
        return true;
    }

here pushId is type of List, which contain all device's push id, at which, notification to be sent.

Problem is that, result does not contain failure push Id or any device identification so that i can identify which device has not received notification. Note:- I don't want to send notification one by one

Response - result

MulticastResult(multicast_id=7588781423174816193,total=5,success=1,failure=4,canonical_ids=0,results: [[ errorCode=NotRegistered ], [ messageId=0:1505731620878025%f02bfbe47eeff668 ], [ errorCode=NotRegistered ], [ errorCode=NotRegistered ], [ errorCode=NotRegistered ]]

which does not contain any registration id or something so that i could identify the device in db.

Please suggest, Thanks in Advance.

The response tells you indirectly with push ids failed.

In your example:

[[ errorCode=NotRegistered ], 
 [ messageId=0:1505731620878025%f02bfbe47eeff668 ], 
 [ errorCode=NotRegistered ],
 [ errorCode=NotRegistered ], 
 [ errorCode=NotRegistered ]]

you can see that the 1st, 3rd, 4th and 5th push IDs failed. Since you are supposed to know in which order you sent the push IDs in your request, you should know which push IDs failed.

Here's a relevant example from the documentation :

Here are JSON results for 6 recipients (IDs 4, 8, 15, 16, 23, and 42 respectively) with 3 messages successfully processed, 1 canonical registration token returned, and 3 errors:

{ "multicast_id": 216, "success": 3, "failure": 3, "canonical_ids": 1, "results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"} ] }

In this example:

  • First message: success, not required.
  • Second message: should be resent (to registration token 8).
  • Third message: had an unrecoverable error (maybe the value got corrupted in the database).
  • Fourth message: success, nothing required.
  • Fifth message: success, but the registration token should be updated in the server database (from 23 to 32).
  • Sixth message: registration token (42) should be removed from the server database because the application was uninstalled from the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM