我无法从GCM的onMessage在Android中打开一个对话框 [英] I can't open a dialog from GCM onMessage in Android

查看:195
本文介绍了我无法从GCM的onMessage在Android中打开一个对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发送消息给我使用谷歌云通讯Android应用程序我无法弄清楚如何打开一个是或否的对话框(如javasrcript确认框),打开一个网页浏览器,如果他们打是或什么都不做如果他们打没有。

我花了太多的时间和仇恨来告诉你没有失败的code应该是在年底,这个基本的code,但我只是出出主意,并尝试样本太多的变化我在网上找到。我怀疑,他们失败了或者是因为我使用了错误的情况下,或者因为我试图从该服务类做到这一点。

 公共类GCMIntentService扩展GCMBaseIntentService {
@覆盖
保护无效的onMessage(上下文myContext,意图意图){
    // TODO自动生成方法存根
    Log.i(LOG_TAG,GCMIntentService的onMessage称为);
    Log.i(LOG_TAG,消息是:+ intent.getStringExtra(数据));
    JSONObject的O = API.getJSONObj(intent.getStringExtra(数据));
    字符串的URL =;
    字符串消息=;
    尝试 {
            URL = o.getString(URL);
    }赶上(JSONException E){
            // TODO自动生成的catch块
            e.printStackTrace();
    }
    尝试 {
            消息= o.getString(信息);
    }赶上(JSONException E){
    // TODO自动生成的catch块
            e.printStackTrace();
    }

    / * code打开对话框或网站到这里* /
 

解决方案

onMessage方法被称为每当你得到任何通知的时间。有关通知打开一个对话框,您就可以开始在一个的onMessage另一个活动,并把你的code(对于对话框)在Activity.Or你也可以把它通知。像这样: -

 公共类GCMIntentService扩展GCMBaseIntentService
{
   @覆盖
   保护无效的onMessage(上下文myContext,意图意图)
   {
        <您的code取代;
        //如果要直接显示任何对话框。
        意图I =新的意图(myContext,<您Activity.class>);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.putExtra(消息,消息);
        myContext.startActivity(ⅰ);

      //如果您想通过通知显示消息。
      NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
      通知通知=新的通知(R.drawable.notification_icon,arg1.getStringExtra(信息),时);
      字符串标题= context.getString(R.string.app_name);
      意图notificationIntent =新的意图(背景下,<您的活动>的.class);
      //设置的意图,因此它不会开始一个新的活动
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
      PendingIntent意图= PendingIntent.getActivity(上下文,0,notificationIntent,0);
      notification.setLatestEventInfo(背景下,标题,arg1.getStringExtra(信息),意图);
      notification.flags | = Notification.FLAG_AUTO_CANCEL;
      notificationManager.notify(0,通知);
  }
}
 

请让我知道,如果这可以帮助你和结婚圣诞节:-)。

When sending a message to my android app using google cloud messaging I can't figure out how to open a yes or no dialog (like a javasrcript confirm box) that opens a website in the browser if they hit yes or does nothing if they hit no.

I have spent too many hours and hate to show you this basic code without the failing code that should be at the end, but I am just out of ideas and have tried too many variations of samples I found online. I suspect that they failed either because I used the wrong context or because I was trying to do it from this service class.

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

解决方案

onMessage method called every time when you got any notification. For opening a dialog box on notification you can start an another activity in onMessage and put your code (for dialog box) in that Activity.Or you can also make it notification. Like this:-

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

Please let me know if this helps you and Marry Christmas :-).

这篇关于我无法从GCM的onMessage在Android中打开一个对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆