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

3
当我使用Google云消息传递向我的Android应用程序发送消息时,我无法弄清如何打开一个“是”或“否”的对话框(就像JavaScript确认框),如果他们选择是,则在浏览器中打开网站,如果他们选择否则不做任何操作。
我已经花了很多时间,很不情愿地向您展示这个基本代码,但我已经没有想法,已经尝试了太多在线样本的变化。我怀疑它们失败是因为我使用了错误的上下文,或者因为我试图从这个服务类中进行操作。
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 */

为什么不改成状态栏通知呢?这样会更加不显眼,而且可以从服务中启动。 - A--C
在您的应用程序中创建一个新活动,该活动仅包含对话框。如果是,则打开网站;如果不是,则关闭您的应用程序。在GCM接收器中使用意图调用特定活动。 - appukrb
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);
  }
}

0

尝试这个方法,将上述代码写入GCMIntentService中的generate notification函数中

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

当点击通知时,它会跳转到您应用程序中的活动,在该活动中,您可以创建对话框并将任何想要放置在活动中的内容放入其中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接