3

Google クラウド メッセージングを使用して Android アプリにメッセージを送信するときに、はいまたはいいえのダイアログ (javasrcript の確認ボックスなど) を開く方法がわかりません。番号。

私はあまりにも多くの時間を費やし、最後にあるはずの失敗したコードなしでこの基本的なコードをお見せするのが嫌いですが、私はアイデアがなく、オンラインで見つけたサンプルのバリエーションをあまりにも多く試しました. 間違ったコンテキストを使用したか、このサービス クラスから実行しようとしていたために失敗したと思われます。

public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onMessage( Context myContext, Intent intent ) {
    // TODO Auto-generated method stub
    Log.i( LOG_TAG, "GCMIntentService onMessage called" );
    Log.i( LOG_TAG, "Message is: " + intent.getStringExtra( "data" ) );
    JSONObject o = API.getJSONObj(intent.getStringExtra( "data" ));
    String URL = "";
    String message = "";
    try {
            URL = o.getString("URL");
    } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
    try {
            message = o.getString("message");
    } catch (JSONException e) {
    // TODO Auto-generated catch block
            e.printStackTrace();
    }

    /* Code to open dialog or website goes here */
4

2 に答える 2

9

通知を受け取るたびに onMessage メソッドが呼び出されます。通知時にダイアログ ボックスを開くには、onMessage で別のアクティビティを開始し、そのアクティビティにコード (ダイアログ ボックス用) を配置します。または、通知することもできます。このような:-

public class GCMIntentService extends GCMBaseIntentService
{
   @Override
   protected void onMessage( Context myContext, Intent intent) 
   {
        <your code>
        //if you want to show any dialog directly.
        Intent i = new Intent(myContext,<your Activity.class>);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.putExtra("message", message);
        myContext.startActivity(i); 

      //if you want to show message through notification.
      NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notification = new Notification(R.drawable.notification_icon,arg1.getStringExtra("message"), when);
      String title = context.getString(R.string.app_name);
      Intent notificationIntent = new Intent(context,<Your Activity>.class);
      // set intent so it does not start a new activity  
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
      PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
      notification.setLatestEventInfo(context, title, arg1.getStringExtra("message"), intent);
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      notificationManager.notify(0, notification);
  }
}

これがあなたと結婚のクリスマスに役立つかどうか教えてください:-)。

于 2012-12-24T06:02:13.620 に答える
0

この作業を試してください。GCMIntentService で、通知の生成に上記のコードを記述します。

private static void generateNotification(Context context, String message) {
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.appicon,
                message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context,
                YourClassName.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

通知をクリックすると、アプリケーションのアクティビティに移動します。その後、アクティビティでダイアログボックスを作成し、アクティビティ内に入れたいものを何でも作成できます

于 2012-12-24T05:52:46.230 に答える