没有在android中接收推送通知

时间:2014-10-16 10:08:27

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

我为android配置了gcm pushnotification客户端接收器。服务器正在接收来自谷歌的发送成功响应,但该消息未在客户端设备接收。应用服务器使用gcm_on_rails gem发送推送通知(gcm_on_rails link:https://github.com/dondeng/gcm_on_rails实现)。我被困在这一点好几天了,任何帮助都会受到赞赏。

这是我的清单文件:

<?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 个答案:

答案 0 :(得分:0)

请注意,在以下代码中:

     // 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());