2012-03-13 99 views
0

我的问题是,我能够从服务器获取通知,但来自服务器的消息不刷新我的手机任何通知是第一次相同的消息我得到所有时间 。这是我的C2DMMessageReceiver代码& MessageReceivedActivity如下。消息不刷新推送通知

public class C2DMMessageReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     Log.w("C2DM", "Message Receiver called"); 
     if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) { 
      Log.w("C2DM", "Received message"); 
      final String payload = intent.getStringExtra("payload"); 
      createNotification(context, payload); 
     } 
    } 
    public void createNotification(Context context, String payload) { 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.icon, 
       "Message received", System.currentTimeMillis()); 
     // Hide the notification after its selected 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     Intent intent = new Intent(context, MessageReceivedActivity.class); 
     intent.putExtra("payload", payload); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       intent, 0); 
     notification.setLatestEventInfo(context, "Message", 
       "New message received", pendingIntent); 
     notificationManager.notify(0, notification); 

    } 

} 

public class MessageReceivedActivity extends Activity 
{ 

@Override 

protected void onCreate(Bundle savedInstanceState) { 

setContentView(R.layout.activity_result); 

Bundle extras = getIntent().getExtras(); 

if (extras != null) { 

String message = extras.getString("payload"); 


if (message != null && message.length() > 0) { 

TextView view = (TextView) findViewById(R.id.result); 

view.setText(message); 

} 

} 

super.onCreate(savedInstanceState); 

} 
} 
+0

你能提供更多的代码吗?像你把这个代码放在哪里? “onCreate”或onResume以及您做了哪种类型的推送? – daigoor 2012-03-13 15:14:10

+0

好吧diagor可以给我你的电子邮件ID? – user1249134 2012-03-14 04:09:06

回答

0

,我可以看到它你的问题是缓存...

例如在我的服务器使用PHP我用这个标题之前,我对输出发送任何

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Cache-Control: post-check=0, pre-check=0", false); 
    header("Pragma: no-cache"); 

希望这可以帮助您

最好的问候, 范吉利斯

+0

hello vangelis我应该在哪里写我的android程序中的代码? – user1249134 2012-03-14 04:14:09

+0

你好, 这不是在android端...我在我的服务器端使用此代码使用php我的需要... 我想你使用你的android应用程序来从远程服务器请求数据吧? 如果是的话,你应该把它放在你发送响应的文件的顶部(如果你使用php),如果你使用另一种语言(asp.net C#等),你应该在互联网上搜索你的服务器语言的缓存技术。 .. 希望这可以帮到你 最好的问候, Vangelis – Vangelis 2012-03-14 08:44:29

0

,因为我看到您需要在您的活动中注册C2DMMessageReceiver尝试覆盖onResume()方法并在此Intent上注册Receiver“com.google.android.c2dm.intent.RECEIVE”。