【问题标题】:Push Notification Sent From Server But not showing up on any android device从服务器发送的推送通知但未显示在任何 android 设备上
【发布时间】:2016-08-18 06:44:11
【问题描述】:

我正在尝试向我的 Android 应用程序添加推送通知,但它似乎不起作用。这是我能做的:

  1. 我的客户端应用程序成功接收到它的registrationId

  2. 我的服务器成功接收到它的身份验证令牌。

  3. 服务器使用其身份验证令牌和设备的registrationId 发送消息。

  4. 从服务器向客户端发送消息时,我收到一条成功的消息响应代码(例如 id=0:1335303367614556%fd55792500000030 响应代码:200,根据 Google 关于 C2DM 的文档,这应该是一条成功消息)。

但是我的设备上没有任何显示,而是每当我发送推送通知时,我的 logcat 中都会出现错误,它们是:

08-18 11:17:53.824 11070-11070/example.haris.com.examplelayout E/GcmReceiver: Failed to resolve target intent service, skipping classname enforcement 08-18 11:17:53.825 11070-11070/example.haris.com.examplelayout E/GcmReceiver: Error while delivering the message: ServiceIntent not found. 08-18 11:17:53.829 11070-11070/example.haris.com.examplelayout E/FirebaseInstanceId: Failed to resolve target intent service, skipping classname enforcement 08-18 11:17:53.830 11070-11070/example.haris.com.examplelayout E/FirebaseInstanceId: Error while delivering the message: ServiceIntent not found.

这是我用来实现上述功能的 3 个类:

GCMPushReceiverService.java

http://i.stack.imgur.com/twLKF.png

GCMRegistrationIntentService

http://i.stack.imgur.com/DfTbm.png

GCMTokenRefreshListenerService.java

public class GCMTokenRefreshListenerService extends InstanceIDListenerService {
/**
 * When token refresh, start service to get new token
 */
@Override
public void onTokenRefresh() {
    Intent intent = new Intent(this, GCMRegistrationIntentService.class);
    startService(intent);
}
}

这是我使用 BroadcastReciever 的主要活动代码:

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Check type of intent filter
            if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
                //Registration success
                String token = intent.getStringExtra("token");
                Toast.makeText(getApplicationContext(), "GCM token:" + token, Toast.LENGTH_LONG).show();
            } else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
                //Registration error
                Toast.makeText(getApplicationContext(), "GCM registration error!!!", Toast.LENGTH_LONG).show();
            } else {
                //Tobe define
            }
        }
    };

    //Check status of Google play service in device
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    if(ConnectionResult.SUCCESS != resultCode) {
        //Check type of error
        if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            Toast.makeText(getApplicationContext(), "Google Play Service is not install/enabled in this device!", Toast.LENGTH_LONG).show();
            //So notification
            GooglePlayServicesUtil.showErrorNotification(resultCode, getApplicationContext());
        } else {
            Toast.makeText(getApplicationContext(), "This device does not support for Google Play Service!", Toast.LENGTH_LONG).show();
        }
    } else {
        //Start service
        Intent itent = new Intent(this, GCMRegistrationIntentService.class);
        startService(itent);
    }

这是我的 AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="example.haris.com.examplelayout.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission
    android:name="example.haris.com.examplelayout.permission.C2D_MESSAGE" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Login.MainScreen">
        <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>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="example.haris.com.examplelayout" />
            <!-- Receives the registration id. -->
           <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        </intent-filter>
    </receiver>

    <service android:name=".GCMPushReceiverService" android:exported="false">
        <intent-filter android:priority="1000">
            <action android:name="com.google.android.c2dm.intent.RECIEVE"></action>
        </intent-filter>
    </service>

    <service android:name=".GCMTokenRefreshListenerService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.idd.InstanceID"></action>
        </intent-filter>
    </service>
    <service
        android:name=".GCMRegistrationIntentService"
        android:exported="false">
        </service>

编辑:

更新清单

<receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="example.haris.com.examplelayout" />
            <!-- Receives the registration id. -->
           <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        </intent-filter>
    </receiver>

    <service android:name="example.haris.com.examplelayout.GCMPushReceiverService" android:exported="false">
        <intent-filter android:priority="1000">
            <action android:name="com.google.android.c2dm.intent.RECIEVE"></action>
        </intent-filter>
    </service>

    <service android:name="example.haris.com.examplelayout.GCMTokenRefreshListenerService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.idd.InstanceID"></action>
        </intent-filter>
    </service>
    <service
        android:name="example.haris.com.examplelayout.GCMRegistrationIntentService"
        android:exported="false">
        </service>

【问题讨论】:

  • onMessageReceived() 是否被调用?
  • 我认为是,如何确认?
  • 放个log语句就好了。
  • 我有,没有显示任何东西!!!
  • 您是否在 Manifest 文件中正确声明了应用的包名称?就像在这篇 SO 帖子中一样 - Android GCM server sent but GCM not pushing to device

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


【解决方案1】:

用完整的包名和子包更新你的清单文件

 <service android:name="example.haris.com.examplelayout.Utility.GCMPushReceiverService" android:exported="false">
    <intent-filter android:priority="1000">
        <action android:name="com.google.android.c2dm.intent.RECIEVE"></action>
    </intent-filter>
 </service>

<service android:name="example.haris.com.examplelayout.Utility.GCMTokenRefreshListenerService" android:exported="false">
    <intent-filter>
        <action android:name="com.google.android.gms.idd.InstanceID"></action>
    </intent-filter>
</service>

<service
    android:name="example.haris.com.examplelayout.Utility.GCMRegistrationIntentService"
    android:exported="false">
    </service>

编辑

<receiver
    android:name=".ExternalReceiver "
    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" />
    </intent-filter>
</receiver>

创建新类

public class ExternalReceiver extends BroadcastReceiver {
    String TAG = "ExternalReceiver";

    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "Got push notification inside onReceive: ");
    }
}

【讨论】:

  • 你能给我看一下包结构屏幕截图和更新的清单文件吗
  • 项目结构截图:i.stack.imgur.com/j5fkS.png 也检查我上面的编辑。
  • 这些类在实用程序子包之外,直接在主包下。
  • 将优先级更改为 10000 并重试
  • 我不会问这个问题,但是请您Ctrl+单击清单文件中的 GCMPushReceiverService 是否打开 GCMPushReceiverService 文件
猜你喜欢
  • 2016-05-06
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2020-09-14
  • 2013-12-07
  • 2016-03-08
相关资源
最近更新 更多