Android push notification GCM

2019-09-19 06:35发布

问题:

I am working on android GCM for my application to send push notification to users. And I am following this tutorial

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

But in This tutorial its shown that we can send push notification to individual device. But I want to send push notification to all the users at once. So how can I implement this technique.

回答1:

I have already faced the same problem and solved it. To send push notification to all the users at once, you have to do all the things listed on the tutorial you've mentioned. But you have to change two files: index.php and send_message.php

  1. In index.php:

    a. Replace the JavaScript code with the following:

    $(document).ready(function(){
    
    });
    function sendToAll(){
        var data = $("#sendtoall").serialize();
        $("#sendtoall").unbind('submit');               
        $.ajax({
            url: "send_message.php",
            type: 'GET',
            data: data,
            beforeSend: function() {
    
            },
            success: function(data, textStatus, xhr) {
                  $('.txt_message').val("");
            },
            error: function(xhr, textStatus, errorThrown) {
    
            }
        });
        return false;
    }
    

    b. Replace the code inside body tag with the following (skipping first five lines):

    <div class="container">
        <h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
        <hr/>
        <ul class="devices">
            <?php
            if ($no_of_users > 0) {
                ?>
                <li>
                    <form id="sendtoall" name="" method="post" onsubmit="return sendToAll()">
                        <div class="send_container">                               
                            <textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
                            <?php
                            while ($row = mysql_fetch_array($users)) {
                            ?>
                            <input type="hidden" name="regId[]" value="<?php echo $row["gcm_regid"]; ?>"/>
                            <?php
                            }
                            ?>
                            <input type="submit" class="send_btn" value="Send To All" onclick=""/>
                        </div>
                    </form>
                </li>
                <?php
            } else { ?>
                <li>
                    No Users Registered Yet!
                </li>
            <?php } ?>
        </ul>
    </div>
    <div class="clear"></div>
    
  2. In send_message.php:

    Replace the line:

    $registatoin_ids = array($regId);
    

    with

    $registatoin_ids = $regId; 
    


回答2:

You can also take a "backend as a service" where the push module is integrated. So you can notify all participants from the backend. I have some time used "apiomat" and was satisfied



回答3:

Yes you can Send With GCM as it allows Multicast Messagin service also.

with Using Below Formate:

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : {
    ...
  },
}

Here value of key registration_ids should be as follow :

A string array with the list of devices (registration IDs) receiving the message. It must contain at least 1 and at most 1000 registration IDs. To send a multicast message, you must use JSON. For sending a single message to a single device, you could use a JSON object with just 1 registration id, or plain text (see below). Required.

Hope it Will help you.



回答4:

GCM topic messaging allows your app server to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports up to one million subscriptions per app. The app server sends messages with payloads up to 2KB to the topic, and GCM handles the message routing and delivers the message reliably to the right devices. For example, users of a weather forecasting app could opt in to a "severe weather alerts" topic and receive notifications of storms threatening specified areas. for sending a message to all user, you can use Topics.
Source: Google Developer website

and if you take a look you can see all users are registering to a topic. by the topics you can send to all users with a topic.

for sending to topic you have to use this page https://developers.google.com/cloud-messaging/topic-messaging

and for testing the push notifications, you can use "advanced rest client" in chrome extensions.