2011-12-26 104 views
1

我正在製作一個Android應用程序,我想在其中顯示推送通知。我已經實現了c2dm的代碼。c2dm通知未收到

但它只是給註冊ID,但沒有顯示通知。

我使用以下爲其編寫代碼:

的活動:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); 
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate 
    registrationIntent.putExtra("sender", "[email protected]"); 
    registrationIntent.setPackage("com.google.android.gsf"); 
    startService(registrationIntent); 

而在reciever:

 public void onReceive(Context context, Intent intent) 
    { 
    String action = intent.getAction(); 
    System.out.println("action is " + action); 
    Log.w("C2DM", "Registration Receiver called"); 
    if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) 
     { 
     Log.w("C2DM", "Received registration ID"); 
     registrationId = intent.getStringExtra("registration_id"); 
     String error = intent.getStringExtra("error"); 

     Log.d("C2DM", "dmControl: registrationId = " + registrationId + ", error = " + error); 
     String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID); 
     sendRegistrationIdToServer(deviceId, registrationId); 

     } 

    else if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) 

     { 
     handleMessage(context, intent); 
     createNotification(context, registrationId); 
     } 
    } 


       public void createNotification(Context context, String registrationId) 
    { 
    URL url; 
    try 
     { 
     NotificationManager notificationManager = 
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = 
       new Notification(R.drawable.icon, "Registration successful", 
         System.currentTimeMillis()); 
     String notificationTitle = "notification"; 
     String notificationText = "New Notification from Bingo Diary"; 
     // Hide the notification after its selected 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     Intent intent = new Intent(context, BingoDiaryActivity.class); 
     intent.putExtra("registration_id", registrationId); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     //notification.setLatestEventInfo(context, "Registration", 
     //  "Successfully registered", pendingIntent); 
     notificationManager.notify(99, notification); 
     notification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent); 


     } 
    catch (Exception e) 
     { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 

誰能告訴我在哪裏,可能是這個問題? 謝謝

回答

0

問題可能與發送方。當你從服務器發送消息時,你會得到什麼?