3

我目前正在开发一个统一插件,它将利用 GCM 进行推送通知,但似乎无法解决这个问题。我按照谷歌开发者网站上的步骤,设法让应用程序注册并从服务器接收消息。但是,一旦 GcmReceiver 收到消息,我的 logcat 就会输出以下错误:

无法启动服务 Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=com.test.app.appname (has extras) } U=0: not found

以下是我用于插件的清单:

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

    <permission android:name="com.test.app.appname.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.test.app.appname.permission.C2D_MESSAGE" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
	
	 <!-- This app has permission to register and receive data message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name">

        <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="com.test.app.appname" />
            </intent-filter>
        </receiver>
        <service
            android:name="com.test.app.appname.MyGCMListenerService"
            android:exported="false"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.android.c2m.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:enabled="true"
            android:name="com.test.app.appname.MyRegistrationIntentService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.test.app.appname.GCMREGISTER" />
            </intent-filter>
        </service>
        <activity
            android:name=".AndroidAPI"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:screenOrientation="sensorLandscape"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:host ="startActivity" android:scheme ="LOHengqa"  />
            </intent-filter>
        </activity>
    </application>
</manifest>

我想知道的一件事是我是否需要创建自己的自定义接收器。从谷歌开发人员的教程中,您似乎可以使用 android.gms.gcm.GcmReceiver,这会触发 GCMListener 上的调用。然而,从我正在经历和尝试调试代码的情况来看,我不知道这是否正确。我的 GCMListenerService 究竟是如何被触发的?我是否缺少将接收器连接到 GCMListenerService 的部分?

顺便说一下,这是 MyGCMListenerService 的代码

/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.test.app.appname;
import static com.test.app.appname.CommonUtilities.APP_CONTEXT;
import static com.test.app.appname.CommonUtilities._GetString;
import static com.test.app.appname.CommonUtilities._GetDrawable;

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 MyGCMListenerService extends GcmListenerService
{
    private static final String TAG = "MyGCMListService";

    /**
     * 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) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);

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

        sendNotification(message);
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param message GCM message received.
     */
    private void sendNotification(String message) {

        final String appId = this.getPackageName();
        long when = System.currentTimeMillis();

        Intent intent = APP_CONTEXT.getPackageManager().getLaunchIntentForPackage(appId);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(_GetDrawable(APP_CONTEXT, "notification_icon"))
                .setContentTitle(_GetString(APP_CONTEXT, "app_name"))
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int)(when % 1000), notificationBuilder.build());
    }
}

4

1 回答 1

0
<service
   android:name="com.test.app.appname.MyGCMListenerService"
   android:exported="false"
    android:enabled="true">
    <intent-filter>
        <action android:name="com.google.android.c2m.intent.RECEIVE" />
    </intent-filter>
</service>

具体来说,我看到:com.google.android.c2m.intent.RECEIVE应该是com.google.android.c2dm.intent.RECEIVE.

似乎如果有人遇到这个问题,它与清单有关。

于 2016-02-27T17:27:47.923 回答