2012-11-12 47 views
1

Hy guys。我在我的应用中实施了Google云消息传递,但是当我尝试将从服务器接收到的消息显示给用户时,我遇到了问题。 我已经设法显示在吐司消息,但我想使用警报对话框。 这是我GCMBaseIntentService: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认为上下文是问题,但我真的不知道如何解决它。 如果我为此使用通知会更好?

回答

-1

我已经解决了使用notifications.I想这问题将不得不做:)

@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()); 
     }