【问题标题】:App Engine with Google Cloud Messaging带有 Google 云消息传递的 App Engine
【发布时间】:2016-07-01 17:41:31
【问题描述】:

我使用 Google Cloud Messaging 实现了应用引擎模块,设备已成功注册,并且我已经实时部署了它。我的问题是,通过使用这种集成做一个 RESTful API,我是否需要制作服务器端代码来发送推送通知,还是可以通过客户端部分发送它们?

这是我的 Android 清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myPackage">
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.Camera2"
        android:required="true" />
    <!-- This filters devices that don't support sip -->
    <uses-feature
        android:name="android.hardware.sip.voip"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.wifi"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.microphone"
        android:required="true" />
    <receiver
        android:name=".IncomingCallReceiver"
        android:label="Call Receiver" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- [START gcm_permission] -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" >
    <!-- [END gcm_permission] -->
    <permission
        android:name="myPackage.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="myPackage.permission.C2D_MESSAGE" />
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <activity android:name=".LogInActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_app_id" />
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity android:name=".MainActivity" />
        <activity
            android:name=".Messaging.Activities.FriendsRequestsActivity"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".Messaging.Activities.ChatActivity"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity"
            android:screenOrientation="portrait"
            android:theme="@style/ChatTheme"
            android:windowSoftInputMode="adjustResize|stateHidden">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="myPackage" />
            </intent-filter>
        </receiver>

        <service
            android:name=".Utils.InstanceIDListener"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

        <service
            android:name="myPackage.Utils.RegistrationIntent"
            android:exported="false"></service>

        <service
            android:name="myPackage.Utils.GcmListener"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
    </application>

</manifest>`

这是我的 GcmListener 接收应该接收消息:

public class GcmListener extends GcmListenerService {

    private static final String TAG = "Push";

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
        JSONObject jsonObject = new JSONObject();
        Log.d(TAG, "Push received");
        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));//TODO Forward to be written
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (from.startsWith("/topics/")) {
            // message received from some topic.
        } else {
            // normal downstream message.
        }

        // [START_EXCLUDE]
        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating to the user
         * that a message was received.
         */
        //sendNotification(message); TODO create the correct notification to be showed to the user
        // [END_EXCLUDE]
    }
}

sendNotification 方法还没有写,但是我放了一个日志来检查是否收到消息,但我什么也没得到,老实说,我认为我使用了错误的 Google Developers Console 的 API_KEY,我使用的是: 服务器密钥(由 Google 服务自动创建),或者可能是清单中服务声明的顺序。这些是我能想到的唯一可能导致应用程序无法收到推送消息的事情

【问题讨论】:

  • 您可以使用本教程(答案已被接受)检查您的 API_KEY 或注册 ID 是否错误.. 之后请告诉我:stackoverflow.com/questions/22168819/…
  • 我收到此消息:未经授权的错误 401
  • 当我将服务器 API_KEY 更改为 Android API_KEY 时,我得到以下信息: {"multicast_id":4751777491966629353,"success":0,"failure":1,"canonical_ids":0,"results" :[{"error":"MismatchSenderId"}]}
  • 这意味着你的项目ID是错误的。你可以去谷歌项目网站再检查一下
  • 我做错了,这是我使用项目 ID 而不是项目编号...非常感谢!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!你太棒了!

标签: android google-cloud-messaging


【解决方案1】:

如果您只想在两个客户端设备之间发送消息,则无需构建自己的服务器。只需发送带有对手注册ID的消息,他们就会收到。无论如何,如果您想存储该消息,您应该构建一个。

你可以像这样创建一个对象,把你要发送的registration_ids和数据放进去

public class MessageSenderContent implements Serializable {
    private List<String> registration_ids;
    private Map<String, String> data;

    public void addRegId(String regId){
        if (registration_ids==null){
            registration_ids = new LinkedList<String>();
            registration_ids.add(regId);
        }
    }

    public void createData(String title,String message) {
        if (data == null)
            data = new HashMap<String, String>();

        data.put("title", title);
        data.put("message", message);
    }

    public Map<String, String> getData() {
        return data;
    }

    public void setData(Map<String, String> data) {
        this.data = data;
    }

    public List<String> getRegIds() {
        return registration_ids;
    }

    public void setRegIds(List<String> regIds) {
        this.registration_ids = regIds;
    }

    @Override
    public String toString() {
        return "MessageSenderContent{" +
                "registration_ids=" + registration_ids +
                ", data=" + data +
                '}';
    }

}

更新 1 您需要像这样将数据放入该对象并向服务器发出 http 请求:

 private static MessageSenderContent createMegContent(String regId, String title) {
        String message = txt_chat.getText().toString();
        MessageSenderContent mgsContent = new MessageSenderContent();
        mgsContent.addRegId(regId);// add registration_id of the device you want to send
        mgsContent.createData(title, message);
        return mgsContent;
    }

更新 2

这是我的类,它将向服务器发送 http 请求:

public class MessageSender {

    private int responseCode;

    public boolean sendPost(MessageSenderContent content) {

        HttpURLConnection connection;
        try {
            URL gcmAPI = new URL("https://android.googleapis.com/gcm/send");
            connection = (HttpURLConnection) gcmAPI.openConnection();

            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type","application/json");
            connection.setRequestProperty("Authorization", "key=" + AppConfig.API_KEY); //api_key is what generate when you register a project  in google console
            connection.setDoOutput(true);
            ObjectMapper mapper = new ObjectMapper();         mapper.setVisibility(PropertyAccessor.FIELD,JsonAutoDetect.Visibility.ANY);
            DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());

            mapper.writeValue(dataOutputStream, content);

            dataOutputStream.flush();
            dataOutputStream.close();

            responseCode = connection.getResponseCode();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (responseCode == 200) {
            Log.i("Request Status", "This is success response status from server: " + responseCode);
            return true;
        } else {
            Log.i("Request Status", "This is failure response status from server: " + responseCode);
            return false;
        }
    }
}

在我的活动中,我将创建它的一个实例并使用它将 MessageSenderContent 发送到 GCM 服务器,如下所示:

                    MessageSender mgsSender = new MessageSender();
                    new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... params) {
                            MessageSenderContent mgsContent = createMegContent(registration_Id, your_title);

                            mgsSender.sendPost(mgsContent)
                            return null;
                        }
                    }.execute();

服务清单代码:

        <service
            android:name="training.com.services.MessageListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </service>

【讨论】:

  • 抱歉我的无知,但我怎样才能将此消息对象与 Google Cloud Messaging 连接以发送它?
  • 您好,我想我非常了解您的答案,我只是不清楚为什么 createMegContent 返回 MessageSenderContent,以及如何让 GCM 使用获得的令牌发送相应的消息跨度>
  • 哇,很好的答案!!!!!!还有两个小问题,什么是 AppConfig.GCM_API 和 AppConfig.APi_KEY,我想最后一个是通用应用程序的 API Key,我必须放在清单中
  • 我保存的一些全局变量可以在我的应用程序的任何地方使用 :) GCM_API 只是:android.googleapis.com/gcm/send 和 API_KEY 是在您在 google 注册应用程序时生成的
  • 再更新答案,你可以看看两个变量
猜你喜欢
  • 1970-01-01
  • 2015-10-08
  • 2018-05-27
  • 2015-05-22
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
相关资源
最近更新 更多