2015-06-05 86 views

回答

0

最後我發現我的錯誤。

我試圖在廣播接收器中獲取通知消息,但是它錯了我在這個服務onMessage(上下文上下文,意圖數據)中獲取消息。

public class GCMIntentService extends GCMBaseIntentService{ 

private static final String TAG = "GCMIntentService"; 

public GCMIntentService() { 
    super(SENDER_ID); 
} 

@Override 
protected void onError(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 

} 

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

    Log.i(TAG, "new message= "); 
     String message = data.getExtras().getString("message"); 
     /*if (message.equals("")) { 
      //message="hellow senha , this is vc circle notification. You can check how it display long or short..."; 
      generateNotification(context, message); 
     }else{ 
      generateNotification(context, message); 
     }*/ 
     //message="hellow senha , this is vc circle notification. You can check how it display long or short..."; 
     generateNotification(context, message); 
} 

private void generateNotification(Context context, String message) { 

    int icon = R.drawable.vc_placeholder; 
     long when = System.currentTimeMillis(); 
     NotificationManager notificationManager = (NotificationManager) context 
     .getSystemService(Context.NOTIFICATION_SERVICE); 

     Intent notificationIntent = new Intent(context, SplashScreenActivity.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
     | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, 
     notificationIntent, 0); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       context); 

     Notification notification = mBuilder.setSmallIcon(icon).setTicker("VCCircle").setWhen(when) 
       .setContentTitle(context.getString(R.string.app_name)) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
       .setContentIntent(resultPendingIntent) 
       .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
       .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.vc_placeholder)) 
       .setContentText(message).build(); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notificationManager.notify(0, notification); 

     { 
      // Wake Android Device when notification received 
      PowerManager pm = (PowerManager) context 
        .getSystemService(Context.POWER_SERVICE); 
      final PowerManager.WakeLock mWakelock = pm.newWakeLock(
        PowerManager.FULL_WAKE_LOCK 
          | PowerManager.ACQUIRE_CAUSES_WAKEUP, "GCM_PUSH"); 
      mWakelock.acquire(); 

      // Timer before putting Android Device to sleep mode. 
      Timer timer = new Timer(); 
      TimerTask task = new TimerTask() { 
       public void run() { 
        mWakelock.release(); 
       } 
      }; 
      timer.schedule(task, 5000); 
     } 



} 

@Override 
protected void onRegistered(Context context, String registrationId) { 
    Log.i(TAG, "Device registered: regId = " + registrationId); 
    displayMessage(context, getString(R.string.gcm_registered)); 
    boolean te=ServerUtilities.register(context, registrationId); 
    System.out.println("Device Register : "+te); 
} 

@Override 
protected void onUnregistered(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 

} 

}

相關問題