GCM通知GCMBaseIntentService方法generateNotification()没有正常运行



这是我的GCMBaseIntentService Java文件。

 public class GCMIntentService extends GCMBaseIntentService {
            private static final String TAG = "GCMIntentService";
            public GCMIntentService() {
                super(SENDER_ID);
            }

            @Override
            protected void onRegistered(Context context, String registrationId) {
                Log.i(TAG, "Device registered: regId = " + registrationId);
                displayMessage(context, "Your device registred with GCM");
                Log.d("GCMRegister", "GCMRegisterSucess");
            }

            @Override
            protected void onUnregistered(Context context, String registrationId) {
                Log.i(TAG, "Device unregistered");
                displayMessage(context, getString(R.string.gcm_unregistered));
            }

            @Override
            protected void onMessage(Context context, Intent intent) {

                Log.d(TAG, "Received message");
                String message = intent.getExtras().getString("price");
                displayMessage(context, message);
                // notifies user
                generateNotification(context, message);
            }

            @Override
            protected void onDeletedMessages(Context context, int total) {
                Log.i(TAG, "Received deleted messages notification");
                String message = getString(R.string.gcm_deleted, total);
                displayMessage(context, message);
                // notifies user
                generateNotification(context, message);
            }

            @Override
            public void onError(Context context, String errorId) {
                Log.i(TAG, "Received error: " + errorId);
                displayMessage(context, getString(R.string.gcm_error, errorId));
            }
            @Override
            protected boolean onRecoverableError(Context context, String errorId) {
                // log message
                Log.i(TAG, "Received recoverable error: " + errorId);
                displayMessage(context, getString(R.string.gcm_recoverable_error,
                        errorId));
                return super.onRecoverableError(context, errorId);
            }

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

            }
        }

构建。gradle类

    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 23
        buildToolsVersion "24.0.1"
        defaultConfig {
            applicationId "com.bindaspunch.my.gcmdemo"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile files('libs/gcm.jar')
    }

我没有得到通知,错误显示在这行

   notificationManager.notify(0, notification);

在generateNotification方法..

但是如果我使用

     Notification notification = new Notification.Builder(context)
                    .setContentText(message)
                     .setSmallIcon(icon)
                     .setWhen(when)
                     .build();

代替这一行。

Notification notification = new Notification(icon, message, when);

Notification get with a Icon,但没有得到notificationIntent,也没有打开MainActivity类。

     Intent notificationIntent = new Intent(context, MainActivity.class);

这是我的manifest类

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
      <permission
        android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.androidhive.pushnotifications.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" />
   <activity android:name=".MainActivity">
            <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.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.androidhive.pushnotifications" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />
I'm new in android development and try this gcm notificatyion first time
this is my demo project for GCM Service but is not working properly.
    i have this issue till one day.. 
i tried many solutions from google as well but this issue remains same here as it is now...
    and not get any useful solution for it
    if you have any solution or changes regading this issue, please help to solve it out..
i dont know very much about gcm notification service, so if you have any kind of solution for this issue, jst help me out of it.

i know my english is poor so Sorry for my english..  and Thanks in advance..

尝试取消注释:

 notification.setLatestEventInfo(context, title, message, 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);
  }

或者这样试试:

private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        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);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(title);
        builder.setContentText(message)
        builder.setSmallIcon(icon);
        builder.setContentIntent(intent);
        builder.setWhen(when)
        builder.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND);
        builder.setAutoCancel(true);
        notificationManager.notify(0, builder.build());

}

最新更新