我无法在Android中从GCM onMessage打开对话框

fcipmucu  于 2022-12-28  发布在  Android
关注(0)|答案(2)|浏览(127)

当我使用google cloud messaging向我的android应用程序发送消息时,我不知道如何打开一个yes或no对话框(比如javasrcript确认框),如果点击yes,它会在浏览器中打开一个网站,如果点击no,它什么也不做。
我已经花了太多的时间,我不想在最后没有失败代码的情况下向您展示这些基本代码,但是我实在是没有主意了,并且尝试了我在网上找到的示例的太多变体。我怀疑它们失败的原因要么是我使用了错误的上下文,要么是我试图从这个服务类中完成它。

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 */
eit6fx6z

eit6fx6z1#

每次收到通知时都会调用onMessage方法。要在收到通知时打开对话框,您可以在onMessage中启动另一个Activity,并将您的代码(对话框)放入该Activity中。或者,您也可以将其设置为通知。如下所示:

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

i34xakig2#

尝试此操作,在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);
    }

当单击通知时,它将转到应用程序中的活动,之后您可以在活动中创建对话框以及任何您想要放入活动中的内容

相关问题