2013-03-18 48 views
1

我想在android的通知區域顯示通知消息。現在這個顯示只應用標題。 我正在尋找logcat,但我沒有看到從行System.out.println(newMessage)沒有System.out輸出;如何使用GCM獲取消息發送?

我有這個類:

/** 
* Author  : Taufan E. 
* App Name  : The Restaurant App 
* Release Date : October 2012 
*/ 
package com.the.restaurant; 


public class Home extends Activity { 


    String gcm_regId=""; 

    static DBHelper dbhelper; 
    MainMenuAdapter mma; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home); 




     //GCMRegistrar.unregister(this); 
     // Make sure the device has the proper dependencies. 
       GCMRegistrar.checkDevice(this); 

       // Make sure the manifest was properly set - comment out this line 
       // while developing the app, then uncomment it when it's ready. 
       //GCMRegistrar.checkManifest(this); 



       gcm_regId = GCMRegistrar.getRegistrationId(this); 
       if(gcm_regId.equals("")){ 
        GCMRegistrar.register(this, Utils.SENDER_ID); 
       } 
       System.out.println(gcm_regId); 

       registerReceiver(mHandleMessageReceiver, new IntentFilter("com.the.restaurant.DISPLAY_MESSAGE")); 

     if (!Utils.isNetworkAvailable(Home.this)) { 
      Toast.makeText(Home.this, getString(R.string.no_internet), Toast.LENGTH_SHORT).show(); 
     } 

     mma = new MainMenuAdapter(this); 
     dbhelper = new DBHelper(this); 
     listMainMenu.setAdapter(mma); 

    } 
    @Override 
    protected void onPause(){ 
     super.onPause(); 
     unregisterReceiver(mHandleMessageReceiver); 
    } 

    @Override 
    protected void onResume(){ 
     super.onResume(); 
     registerReceiver(mHandleMessageReceiver, new IntentFilter("com.the.restaurant.DISPLAY_MESSAGE")); 
    } 

    private void displayNotification(final Context theContext,String message){ 
     long when = System.currentTimeMillis(); 
     int icon = R.drawable.cover_image; 

     NotificationManager notificationManager = (NotificationManager) 
       theContext.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     String title = theContext.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(); 
     // 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(theContext, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(theContext, 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 final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String newMessage = intent.getExtras().getString("rezervare"); 

      // Waking up mobile if it is sleeping 
      WakeLocker.acquire(getApplicationContext()); 

      /** 
      * Take appropriate action on this message 
      * depending upon your app requirement 
      * For now i am just displaying it on the screen 
      * */ 

      // Showing received message 
      //lblMessage.append(newMessage + "\n"); 
      System.out.println(newMessage); 
      Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show(); 

      displayNotification(context,newMessage); 
      //sendnotification("The Restaurant App","Mesaj"); 
      //GCMRegistrar.unregister(Reservation.class); 
      // Releasing wake lock 
      WakeLocker.release(); 
     } 
    }; 
} 

回答

0

你需要先構建通知:

// Pop up Notification 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setContentTitle("New Notification Title") 
     .setContentText("This is notification content."); 
// Creates an explicit intent for an Activity in your app 
Intent resultIntent = new Intent(this, ClassToLaunch.class); 

// The stack builder object will contain an artificial back stack for the 
// started Activity. 
// This ensures that navigating backward from the Activity leads out of 
// your application to the Home screen. 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
// Adds the back stack for the Intent (but not the Intent itself) 
stackBuilder.addParentStack(ClassToLaunch.class); 
// Adds the Intent that starts the Activity to the top of the stack 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(
      0, 
      PendingIntent.FLAG_UPDATE_CURRENT 
     ); 

mBuilder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

mNotificationManager.notify("tag", mBuilder.build()); 
+0

謝謝。但這對我並不好。 NotificationCompat需要API 16,我針對較低的API版本(最小爲7)。 – b3n1Am1n 2013-03-19 12:14:10

0

根據你的問題,你不能看到下面一行輸出:

 System.out.println(newMessage); 

而且你想顯示通知,我可以看到將會顯示後,下面的方法是執行:

displayNotification(context,newMessage); 

答:

你期望要執行的代碼是一個BroadcastReceiver的的onReceive方法,這意味着該方法時,一個特殊的意圖產生它的onReceive將被稱爲下已註冊到您的onCreate方法就行了:

registerReceiver(mHandleMessageReceiver, new IntentFilter("com.the.restaurant.DISPLAY_MESSAGE")); 

您可以播放通過以下方式意圖:

Intent intent = new Intent(); 
    intent.setAction("com.the.restaurant.DISPLAY_MESSAGE"); 
    sendBroadcast(intent); ///sendBroadcast is a Context (Activity) method 
+0

我該怎麼做才能正常工作? – b3n1Am1n 2013-03-18 23:22:04

+0

你將不得不播放一個意圖......我編輯了我的答案看看....或者它應該在你的代碼庫搜索活動廣播意圖 – 2013-03-19 09:23:27