2016-02-05 60 views
1

你好,我是想在Android上的推送通知使用GCM服務,但我一直提示錯誤,此代碼GCM Android的錯誤

package com.subhi.gcmdemo; 

    /** 
    * Created by subhi on 2/4/2016. 
    */ 
    import com.google.GoogleCloudMessaging; 
    import android.app.IntentService; 
    import android.app.NotificationManager; 
    import android.app.PendingIntent; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.os.SystemClock; 
    import android.support.v4.app.NotificationCompat; 
    import android.util.Log; 

    public class GcmIntentService extends IntentService { 
    public static final int NOTIFICATION_ID = 1; 
    private static final String TAG = "GcmIntentService"; 
    private NotificationManager mNotificationManager; 
    NotificationCompat.Builder builder; 

    public GcmIntentService() { 
     super("GcmIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Bundle extras = intent.getExtras(); 
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
     // The getMessageType() intent parameter must be the intent you received 
     // in your BroadcastReceiver. 
     String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
      /* 
      * Filter messages based on message type. Since it is likely that GCM 
      * will be extended in the future with new message types, just ignore 
      * any message types you're not interested in, or that you don't 
      * recognize. 
      */ 
      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()); 
       // If it's a regular GCM message, do some work. 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
       // This loop represents the service doing some work. 
       for (int i=0; i<5; i++) { 
        Log.i(TAG, "Working... " + (i+1) 
          + "/5 @ " + SystemClock.elapsedRealtime()); 
        try { 
         Thread.sleep(5000); 
        } catch (InterruptedException e) { 
        } 
       } 
       Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
       // Post notification of received message. 
       sendNotification(extras.getString("Notice")); 
       Log.i(TAG, "Received: " + extras.toString()); 
      } 
     } 
     // Release the wake lock provided by the WakefulBroadcastReceiver. 
     GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 

    // Put the message into a notification and post it. 
    // This is just one simple example of what you might choose to do with 
    // a GCM message. 
    private void sendNotification(String msg) { 
     mNotificationManager = (NotificationManager) 
       this.getSystemService(Context.NOTIFICATION_SERVICE); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         // .setSmallIcon(R.drawable.ic_stat_gcm) 
         .setContentTitle("GCMDemo") 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setStyle(new NotificationCompat.BigTextStyle() 
           .bigText(msg)) 
         .setContentText(msg); 

     mBuilder.setContentIntent(contentIntent); 
     mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
    } 
    } 

,這是清單文件

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <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="com.subhi.gcmdemo.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 
    <uses-permission android:name="com.programmingtechniques.gcmdemo.permission.C2D_MESSAGE" /> 

    <uses-sdk 
    android:minSdkVersion="16" 
    android:targetSdkVersion="22" /> 


    </manifest> 

與我曾使用依賴

compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.google.android.gms:play-services:8.4.0' 

因此,如果有人能幫助我解決這個問題嗎?

+0

錯誤日誌請好嗎? –

回答

0

我檢查了你的AndroidManifest.xml你沒有定義你的廣播接收器和GcmIntentService類作爲服務。

<receiver android:name="com.package .application.service.GcmBroadcastReceiver" 
       android:permission="com.google.android.c2dm.permission.SEND"> 

     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.app.twoway" /> 
     </intent-filter> 

    </receiver> 

    <service android:name="com.package.application.service.GcmIntentService" />