Android GCM。应用程序未收到推送通知

时间:2015-09-13 06:36:00

标签: android push-notification google-cloud-messaging

所以在阅读了很多StackOverflow线程后,没有任何帮助,并阅读了大量的Google文档,我在这里问我是否可以找人找出错误的信息。

我有app应该只在我的服务器上注册InstanceID令牌,并且能够从GCM获得推送通知。

我能够获取令牌并在我的服务器上注册,但我没有收到应用程序的通知。

在logcat中,我可以看到推送到达设备,但没有进入应用程序:

I/GCM﹕ GCM message com.kazav.gabi.pushtest 0:1442125487591772%7dfbbb577dfbbb57

我的一些代码:

AndroidMnifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kazav.gabi.pushtest" >

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <permission android:name="com.kazav.gabi.pushtest.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.kazav.gabi.pushtest.permission.C2D_MESSAGE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/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>

        <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" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.kazav.gabi.pushtest" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.kazav.gabi.pushtest.TokenReload"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>

        <service
            android:name="com.kazav.gabi.pushtest.PushHendler"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECIEVE" />
            </intent-filter>
        </service>
    </application>

</manifest>

我的Push处理程序java文件:

PushHendler.java:

package com.kazav.gabi.pushtest;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

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

public class PushHendler extends GcmListenerService {


    @Override
    public void onMessageReceived(String from, Bundle data) {
        super.onMessageReceived(from, data);
        String message = data.getString("message");
        sendNotification(message);
    }

    private void sendNotification(String message) {
        Log.w("Notification", message);
    }
}

我在onMessageRecieved函数上有断点但是我的应用程序永远不会进入它..

有人看到我的代码有问题吗?

谢谢, 加比。

1 个答案:

答案 0 :(得分:0)

我没有看到GCM实现的GCMIntentService。您可以拥有一个充当服务的代码,并在清单文件中使用。

最重要的方法是 generateNotification ,它通过pending Intent侦听传入的消息并生成通知。确保你添加它。

看一下这段代码:

private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);      

    }

有关其他信息,请查看此tutorial

相关问题