3

当使用谷歌云消息向我的 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 回答