简体   繁体   English

GcmListenerService客户端通知

[英]GcmListenerService client side notification

I am trying to use gcm to push notifications on android devices. 我正在尝试使用gcm在Android设备上推送通知。 So far I have registered with gcm and sent my back-end the necessary information to make a post request to the gcm severs which builds a 200 response. 到目前为止,我已经向gcm注册,并向后端发送了必要的信息,以便向gcm服务器发送发布请求,从而建立200个响应。 All the steps have come together except the client side receiving the message. 除了客户端收到消息外,所有步骤都已合并完成。 I don't get the message. 我没有收到消息。 Displayed here is my gcm listener class: 这里显示的是我的gcm侦听器类:

import com.google.android.gms.gcm.GcmListenerService;

public class MyGcmListenerService extends GcmListenerService {

    public static final int MESSAGE_NOTIFICATION_ID = 435345;

    private static final String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

        sendNotification(from,message);

    }

    private void sendNotification(String title, String body) {
        Context context = getBaseContext();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title)
                .setContentText(body);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
    }

}

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <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" />

    <recieve android:name = ".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="com.example.caesar.gcm" />
    </intent-filter>

</recieve>


</application>

// new manifest file //新清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.example.caesar.gcm.permission.C2D_MESSAGE"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <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" />

    <reciever
        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="com.example.caesar.gcm" />
    </intent-filter>

</reciever>
    <service
        android:name="com.example.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

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

</application>

Actually looking at this further it looks like you haven't setup the receiver correctly. 实际上,进一步看似乎您没有正确设置接收器。 Seems you need to include at least. 似乎您至少需要包括在内。

 <service
            android:name="com.example.MyGcmListenerService "
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

And for the rest just follow that link. 对于其余的,只需单击该链接。

You have to put your resgistration intent service at the end of yout Manifest file. 您必须将注册意向服务放在清单文件的末尾。 Like this example: 像这个例子:

    <service
            android:name="com.example.gcm.RegistrationIntentService"
            android:exported="false">
    </service>

Hope that help you, more information, you can visit this link and you can view an example of RegistrationIntentService here . 希望对您有帮助,更多信息,您可以访问此链接,并可以在此处查看RegistrationIntentService的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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