簡體   English   中英

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

[英]Android GCM. Application not receiving the push notification

因此,在沒有任何幫助的情況下閱讀了很多StackOverflow線程,並閱讀了許多Google文檔之后,我在這里詢問是否可以找人找出問題所在。

我有只應在服務器上使用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函數上有斷點,但我的應用程序從未使用過。

有人看到我的代碼有問題嗎?

謝謝,加比。

我沒有看到您的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);      

    }

有關其他信息,請參閱本教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM