发送使用谷歌GCM推送通知 [英] Sending push notification using Google GCM

查看:406
本文介绍了发送使用谷歌GCM推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注href="https://developers.google.com/cloud-messaging/android/client" rel="nofollow">设置一个GCM客户端应用程序的教程和

上面的引导使我一个生成实例id令牌屏幕,永无止境的进度。该节目源可以在这里获得:HTT PS://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main

使用上述源$ C ​​$ C我旁边,简单的下游短信试过之后,这样的:的 https://developers.google.com/cloud-messaging/downstream 以发送推送的应用程序的用户。

我不明白为什么我应该用它送我的应用程序的用户通知。

我有一个服务器,我很好用PHP,那还剩下什么做才能使用GCM发送推送通知我的应用程序的用户?

终于设法得到消息到一个TextView! 如何使之成为推?

 公共类GcmService扩展GcmListenerService {

    @覆盖
    公共无效onMessageReceived(String一个,捆绑数据){
        的JSONObject的JSONObject =新的JSONObject();
        设置<字符串>键= data.keySet();
        对于(字符串键:键){
            尝试 {
                jsonObject.put(键,data.get(键));
            }赶上(JSONException E){
                e.printStackTrace();
            }
        }
        尝试 {
            sendNotification的(收到:+ jsonObject.toString(5));
        }赶上(JSONException E){
            e.printStackTrace();
        }
    }

    @覆盖
    公共无效onDeletedMessages(){
        sendNotification的(在服务器上删除邮件);
    }

    @覆盖
    公共无效onMessageSent(字符串MSGID){
        sendNotification的(上游邮件发送的ID =+ MSGID);
    }

    @覆盖
    公共无效onSendError(字符串MSGID,字符串错误){
        sendNotification的(上游邮件发送错误ID =+ MSGID +,错误+误差);
    }

    私人无效sendNotification的(最后弦乐味精){
        处理程序处理程序=新的处理程序(Looper.getMainLooper());
        handler.post(新的Runnable(){
            @覆盖
            公共无效的run(){
                如果(MainActivity.mTextView!= NULL){
                    MainActivity.mTextView.setText(MSG);
                }
            }
        });
    }
}
 

解决方案

让我们假设你已经创建的谷歌开发者控制台一个新的项目,并注意到了2个值:项目编号,这将作为SENDER_ID在客户机项目;和(在凭证创建)API服务器密钥,将被用作API_KEY在一个服务器项目

您可以找到更多关于我的答案在下面的问题基本服务器项目。在第二个环节,你会发现样品code中的Java服务器项目,那么你可以参考它的逻辑在你的PHP项目的实施:

<一个href="http://stackoverflow.com/questions/32322631/adding-google-cloud-messagin-gcm-for-android-registration-process/32323334#32323334">Adding谷歌云Messagin(GCM)为Android - 注册过程

<一个href="http://stackoverflow.com/questions/32299997/how-to-implement-a-gcm-hello-world-for-android-using-android-studio">How采用Android工作室实现GCM的Hello World Android版

希望这有助于!

I've been following the Set Up a GCM Client App tutorial and tried to understand the demo app they provided, but I couldn't understand how to send push notification with this service.

The above guide leads me to a "Generating InstanceID token" screen with never-ending ProgressBar. This program source can be obtained here: htt ps://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main

After using the above source code I've tried next to Simple Downstream messaging, this: https://developers.google.com/cloud-messaging/downstream in order to send push to the app users.

I couldn't understand how I'm supposed to send my app users notifications with it.

I have a server and I'm great with PHP, What is left to do in order to send push notification to my application users using the GCM?

Finally managed to get the message to a TextView! How do make it into push?

public class GcmService extends GcmListenerService {

    @Override
    public void onMessageReceived(String from, Bundle data) {
        JSONObject jsonObject = new JSONObject();
        Set<String> keys = data.keySet();
        for (String key : keys) {
            try {
                jsonObject.put(key, data.get(key));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        try {
            sendNotification("Received: " + jsonObject.toString(5));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDeletedMessages() {
        sendNotification("Deleted messages on server");
    }

    @Override
    public void onMessageSent(String msgId) {
        sendNotification("Upstream message sent. Id=" + msgId);
    }

    @Override
    public void onSendError(String msgId, String error) {
        sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
    }

    private void sendNotification(final String msg) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (MainActivity.mTextView != null) {
                    MainActivity.mTextView.setText(msg);
                }
            }
        });
    }
}

解决方案

Let's assume you have created a new project at Google Developers Console and taken note of 2 values: Project Number, which will be used as SENDER_ID in a client project; and API server key (created at Credentials), which will be used as API_KEY in a server project.

You can find more about basic server project with my answers at the following questions. In the second link, you will find sample code for a server project in Java, then you can refer to its logic to implement in your PHP project:

Adding Google Cloud Messagin (GCM) for Android - Registration process

How to implement a GCM Hello World for Android using Android Studio

Hope this helps!

这篇关于发送使用谷歌GCM推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆