在android中没有收到推送通知

Posted

技术标签:

【中文标题】在android中没有收到推送通知【英文标题】:not receiving pushNotification in android 【发布时间】:2014-10-16 10:08:26 【问题描述】:

我已经为 android 配置了 gcm pushnotification 客户端接收器。服务器正在接收来自谷歌的发送成功响应,但客户端设备未接收到消息。应用服务器使用 gcm_on_rails gem 发送推送通知(gcm_on_rails 链接:https://github.com/dondeng/gcm_on_railsimplementation)。我在这一点上被困了几天,任何帮助将不胜感激。

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.glympse.android.sendlite"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- GLYMPSE API - Needed to send Glympses via SMS -->
    <uses-permission android:name="android.permission.SEND_SMS" />

    <!-- GLYMPSE API - Needed to read contacts if using the Recipients editor from the Send Wizard -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <!-- GLYMPSE API - Used to query network connection status before attempting network activity -->
    <!-- Docs:   Allows applications to access information about networks -->
    <!-- Install: View network connections -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- GLYMPSE API - Used to create a default user profile (nickname/avatar) -->
    <!-- Optional but highly recommended -->
    <!-- Docs:    Allows an application to read the user's contacts data. -->
    <!-- Install: Read your contacts -->
    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <!-- GLYMPSE API - Used to create a default user profile (nickname/avatar) -->
    <!-- Optional but highly recommended -->
    <!-- Docs:    Allows an application to read the user's personal profile data. -->
    <!-- Install: Read your own contact card -->
    <uses-permission android:name="android.permission.READ_PROFILE" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />


    <permission
        android:name="com.glympse.android.sendlite.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.glympse.android.sendlite.permission.C2D_MESSAGE" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:theme="@style/AppTheme"
        android:label="@string/app_name" >
        <activity
            android:name=".ChaffeoSplash"            
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:screenOrientation="portrait" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity
            android:name="com.glympse.android.login.MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >            
        </activity>
        <activity
            android:name="com.glympse.android.home.Home"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.glympse.android.acceptrejectrides.ShowMyList"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".IndividualBookings"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".NoRidesAccRej"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.glympse.android.yourrides.YourRides"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".DetailsActivity"
            android:configChanges="orientation|keyboardHidden"
            android:exported="true"
            android:screenOrientation="portrait" />
        <activity
            android:name=".StartRide"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".FullscreenActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".NoRidesYourRides"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".TabGroupActivity"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".TabGroup1Activity"
            android:screenOrientation="portrait" >
        </activity>
        <activity android:name=".TabsActivity" >
        </activity>
        <activity
            android:name=".TabsActivity2"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".TabsActivity1"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".GlympseSendLiteDemoActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize"
            android:screenOrientation="portrait" >
        </activity>

        <!-- GLYMPSE API - This is required to make the GlympseSendWizard configurator work. -->
        <activity
            android:name="com.glympse.android.kit.send.GlympseSendWizard"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize" >
        </activity>

        <!-- GLYMPSE API - This is required to be notified, when Glympse service is restarted. -->
        <receiver
            android:name=".GlympseLiteWrapper$ServiceReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.glympse.android.hal.service.STARTED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".EndRide" >
        </activity>

         <receiver
            android:name="com.glympse.android.notifications.GcmBroadcastReceiver"
            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.glympse.android.sendlite" />
            </intent-filter>
        </receiver>

        <service android:name="com.glympse.android.notifications.GCMNotificationIntentService" />

    </application>

</manifest>

这是我的广播接收器:

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver 

    @Override
    public void onReceive(Context context, Intent intent) 


         // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMNotificationIntentService.class.getName());

         // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);


    

这是我的 GCM 服务类:

    public class GCMNotificationIntentService extends IntentService 

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

    public GCMNotificationIntentService() 
        super("GcmIntentService");
    

    public static final String TAG = "GCMNotificationIntentService";

    @Override
    protected void onHandleIntent(Intent intent) 
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        String msg = intent.getStringExtra("message");

        System.out.println("###############GCM Message type---> " + messageType);
        System.out.println("GCM intent.getStringExtra()---> " + msg);

        if (!extras.isEmpty()) 

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                    .equals(messageType)) 
                sendNotification("Send error: " + extras.toString());
             
            else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                    .equals(messageType)) 
                sendNotification("Deleted messages on server: "
                        + extras.toString());
             
            else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                    .equals(messageType)) 

                for (int i = 0; i < 3; i++) 
                    Log.i(TAG,
                            "Working... " + (i + 1) + "/5 @ "
                                    + SystemClock.elapsedRealtime());
                    try 
                        Thread.sleep(5000);
                     catch (InterruptedException e) 
                    

                
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

//              sendNotification(""+extras.get(DriverUtils.MESSAGE_KEY));


                sendNotification(""+extras.get("message"));

                Log.i(TAG, "Received: " + extras.toString());
                System.out.println("GCM GCM GCM" + extras.toString());

            
        
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    

    @SuppressLint("NewApi")
    private void sendNotification(String msg) 

        System.out.println("MESSAGE : " + msg);             

        // Prepare intent which is triggered if the
        // notification is selected
       Intent intent = new Intent(this, ChaffeoSplash.class);
       PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
            .setContentTitle("Chaffeo")
            .setContentText(msg)
            .setSmallIcon(R.drawable.icon)
            .setContentIntent(pIntent).build();
        noti.flags |= Notification.FLAG_AUTO_CANCEL;// hide the notification after its selected
        noti.defaults |= Notification.DEFAULT_VIBRATE;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // hide the notification after its selected  
        notificationManager.notify(0, noti);
    

谁能帮我解决这个问题,提前谢谢。

【问题讨论】:

【参考方案1】:

注意以下代码:

     // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GCMNotificationIntentService.class.getName());

您是说GCMNotificationIntentService 位于应用程序的主包中。但是,您的GCMNotificationIntentService 位于com.glympse.android.notifications,而您的应用程序的主包是com.glympse.android.sendlite

你可以这样修复它:

     // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(GCMNotificationIntentService.class.getPackage().getName(),
            GCMNotificationIntentService.class.getName());

【讨论】:

以上是关于在android中没有收到推送通知的主要内容,如果未能解决你的问题,请参考以下文章

手机间隙中的android推送通知没有收到通知

有时一些 android 用户没有通过 GCM 收到推送通知

Android 没有收到来自新 Parse Server 的推送通知

当我连接到我的 Wifi 时,在 Android 上没有收到推送通知

在 android (0.59.8) 中没有收到推送通知权限警报,但它在 ios 中工作

Android-应用程序没有收到来自解析平台的推送通知