1

やあみんな。アプリに Google クラウド メッセージングを実装しましたが、サーバーから受信したメッセージをユーザーに表示しようとすると問題が発生します。トーストにメッセージを表示することはできましたが、アラート ダイアログを使用したいと思います。これは私の GCMBaseIntentService です:

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

    }
    }

コメントされた部分は、警告ダイアログを表示しようとする私の試みです。コンテキストが問題だと思いますが、それを修正する方法が本当にわかりません。これに通知を使用した方がよいでしょうか?

4

1 に答える 1

-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());
        }
于 2012-11-12T14:42:57.247 に答える