2011-12-26 78 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

问题可能与发送方。当你从服务器发送消息时,你会得到什么?