2012-04-25 8 views
5

저는 이것이 안드로이드 프로그래밍의 큰 멍청한 행동입니다. 나는 푸시 알림 (http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html)을 위해 Vogella 푸시 알림 튜토리얼을 거의 따랐다. 다른 스택 오버플로 관련 질문을 읽었지만 알림을 받으면 의도를 여는 방법에 대해서는 약간 혼란 스럽습니다.푸시 알림을 클릭 한 후 활동 열기 android

예를 들어 알림을 통해 나를 웹 사이트로 안내하려는 경우 어떻게 작동합니까? 내 MessageReceivedActivity 또는 다른 프로젝트/클래스 모두 함께 가야합니까?

덕분에 여기

내가 내 C2DMMessageReceiver

@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"); 
     Log.d("C2DM", "dmControl: payload = " + payload); 
     // TODO Send this to my application server to get the real data 
     // Lets make something visible to show that we received the message 
     createNotification(context, payload); 

    } 
} 

public void createNotification(Context context, String payload) { 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(R.drawable.ic_launcher, 
      "Message received", System.currentTimeMillis()); 
    // Hide the notification after its selected 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    //adding LED lights to notification 
    notification.defaults |= Notification.DEFAULT_LIGHTS; 

    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); 

} 

} C2DM 또는 클래스가 extentd 기본 리시버에 대한베이스 수신기에서

답변

9

이 시도 알림 클릭에 웹 사이트를 열려면 :

public void createNotification(Context context, String payload) { 
     NotificationManager notificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(R.drawable.ic_launcher, 
       "Message received", System.currentTimeMillis()); 
     // Hide the notification after its selected 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     //adding LED lights to notification 
     notification.defaults |= Notification.DEFAULT_LIGHTS; 

     Intent intent = new Intent("android.intent.action.VIEW", 
     Uri.parse("http://my.example.com/")); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
       intent, 0); 
     notification.setLatestEventInfo(context, "Message", 
       "New message received", pendingIntent); 
     notificationManager.notify(0, notification); 

    } 
+0

푸시 알림을 클릭하면 "새 메시지를 받았습니다"메시지 만 표시됩니다. 보류중인 종원에게 의도를 열어 줄 것을 알려줄 필요가 있을까요? – Kevin

+0

신경 쓰지 마라, 코드를 통해 알아 냈다. 1 개의 작은 물건 만 바꾸어야했습니다. 감사! – Kevin

0

에 대해 가지고있는 코드는 (A handleMessage이)입니다 ::

다음은 activity ::

을 실행하는 핸들 메시지의 샘플 코드입니다. 0
@Override 
    protected void handleMessage(Context context, Intent intent) { 
     String regId = C2DMessaging.getRegistrationId(context); 
     String logKey = this.getClass().getSimpleName(); 
     String title=""; 
     String message=""; 
     if (regId!= null) { 
      if (intent.hasExtra(Constants.TITLE)) { 
       title = intent.getStringExtra(Constants.TITLE); 
      } 
      if(intent.hasExtra(Constants.MESSAGE)){ 
       message = intent.getStringExtra(Constants.MESSAGE); 
      } 
      // TODO Send this to my application server to get the real data 
      // Lets make something visible to show that we received the message 
      if(!title.equals("") && !message.equals("")) 
       createNotificationForMsg(context,title,message); 
     } 
    } 

    @Override 
    public void createNotificationForMsg(Context context,String title,String message) { 
     final String logKey = this.getClass().getSimpleName(); 

     try { 
      NotificationManager notificationManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(R.drawable.icon, 
        "update(s) received", System.currentTimeMillis()); 
      // Hide the notification after its selected 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      //adding sound to notification 
      notification.defaults |= Notification.DEFAULT_SOUND;    

       Intent intent = new Intent(context, YourAlertActivity.class); 

       if(Constants.LOG)Log.d(logKey, Constants.TITLE +": "+ title +" , "+Constants.MESSAGE+": "+message); 
       intent.putExtra(Constants.TITLE, title); 
       intent.putExtra(Constants.MESSAGE, message); 

       PendingIntent pendingIntent = PendingIntent.getActivity(context, Calendar.getInstance().get(Calendar.MILLISECOND), intent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
       notification.setLatestEventInfo(context, "Castrol", 
         title+"update Received", pendingIntent); 
       notificationManager.notify(Calendar.getInstance().get(Calendar.MILLISECOND), notification); 



     } catch (Exception e) { 
//   MessageReceivedActivity.view.setText("createNotificationFor Msg: " 
//     + e.toString()); 
     } 
    } 
+1

죄송하지만베이스 수신기는 의미 하는가 메시지 수신기 또는 등록 수신기를? 나는 액티비티를 확장 한 MessageReceivedActivity 아래로 액티비티가 갈 것이라고 생각했다. 그러나 그것은 Broadcast Receiver를 확장하는 MessageReceiver 아래로 갈 것인가? – Kevin

+0

일부 코드를 추가 할 수 있습니까? –

+0

내 코드를 포함하도록 내 질문을 편집했습니다. 예를 들어, 알림을 클릭하면 www.google.com을 열어 볼까요? 의도와 함께하는 방법을 이해합니다. 보류중인 작동 방식을 알지 못합니다. – Kevin

관련 문제