2012-11-12 2 views
1

안녕하세요. 내 앱에서 Google Cloud Messaging을 구현했지만 서버에서받은 메시지를 사용자에게 표시하려고 할 때 문제가 있습니다. 축배 메시지를 표시 할 수 있지만 경고 대화 상자를 사용하고 싶습니다.GCM- GCMBaseIntentService의 AlertDialog 표시

public class GCMIntentService extends GCMBaseIntentService { 

    public static final String SENDER_ID = "546677596418"; 

    public GCMIntentService() { 
     super(SENDER_ID); 
    } 

    @Override 
    protected void onError(Context arg0, String arg1) { 
     // TODO Auto-generated method stub 
     Log.e("Registration", "Got an error!"); 
     Log.e("Registration", arg0.toString() + arg1.toString()); 
    } 

    @Override 
    protected void onMessage(final Context arg0, final Intent arg1) { 
     // TODO Auto-generated method stub 
     Log.i("Registration", "Got a message!"); 
     Log.i("Registration", arg1.getStringExtra("message")); 


     Handler h = new Handler(Looper.getMainLooper()); 
     h.post(new Runnable(){ 

      public void run() { 
       // TODO Auto-generated method stub 
      Toast.makeText(arg0, arg1.getStringExtra("message"), Toast.LENGTH_LONG).show(); 


       /* String msg = arg1.getStringExtra("message"); 
       if(msg != null){ 
        new AlertDialog.Builder(arg0) 
        .setTitle("New Notification") 
        .setMessage(msg) 
        .setNeutralButton("OK", new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface dialog, int which) { 
           dialog.cancel(); 
          } 
        }).create().show(); 
       } 
       */ 
      }   
     }); 

    } 
    } 

주석 부분은 Dialog.I는 상황이 문제라고 생각 경고를 표시 할 내 시도,하지만 난 정말 그것을 해결하는 방법을 알고하지 않습니다 이 내 GCMBaseIntentService입니다. 알림을 사용하면 좋을까요?

답변

-1

내가 할 :)

@Override 
    protected void onMessage(final Context arg0, final Intent arg1) { 
     // TODO Auto-generated method stub 
     Log.i("Registration", "Got a message!"); 
     Log.i("Registration", arg1.getStringExtra("message")); 
     String message= arg1.getStringExtra("message"); 

     generateNotification(getApplicationContext(), message); 

private void generateNotification(Context context, String message) 
     { 



      // Creates an explicit intent for an Activity in your app 
      Intent resultIntent = new Intent(context, BBCAndroid.class); 

      NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 

      mBuilder.setContentTitle("BBC Android") 
        .setContentText(message) 
        .setSmallIcon(R.drawable.notification); 


      PendingIntent in = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, 0); 
      mBuilder.setContentIntent(in); 

      mNotificationManager.notify(0, mBuilder.build()); 
     } 
것이 추측 notifications.I 사용하여 문제를 해결했습니다