【问题标题】:Show alert dialog instead of notification when recieve push from parse.com console从 parse.com 控制台接收推送时显示警报对话框而不是通知
【发布时间】:2014-10-13 15:44:03
【问题描述】:

我正在尝试向我的 Android 应用程序添加推送通知。

首先我从 parse.com 下载了示例项目,看看它是如何工作的。 当我发送消息或 JSON 时,我可以在通知栏中接收推送通知。 现在我想在通知栏中显示一个警报对话框而不是通知。 我已经搜索过,但即使在解析网站上也没有找到解决方案。

任何帮助,请提前致谢。

【问题讨论】:

    标签: android parsing push-notification parse-platform


    【解决方案1】:

    您好,如果您在 jsonObject 中不使用“title”和“alert”键,那么它不会在通知栏中创建默认通知。我可以制作自定义通知而不是默认通知。

    您可以看到我的代码并从警报对话框代码中更改自定义通知代码。看看:

    发送通知的代码:

    String  otherChannelName = "channel" + user_id;
                // send push notfication
            String str_objid = PreferenceSetting.getObjId(mActivity);
            String str_uname = PreferenceSetting.getUName(mActivity);
    
                ParsePush push = new ParsePush();
                push.setChannel(otherChannelName);
    
                push.setMessage(message);
    
                JSONObject data = null;
                try {
                    data = new JSONObject("{\"action\": \""
                            + NChatActivity.BUDDY_CHAT_PUSH_ACTION + "\"" + ",\"from\": \""
                            + str_objid + "\"" + ",\"to\": \"" + user_id
                            + "\"" + ",\"a\" : \"" + message + "\""
                            + ",\"t\" : \"" + type + "\"" + "}");
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                push.setData(data);
    
                push.sendInBackground();
    

    接收通知的代码:

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
    
    
            Bundle extras = intent.getExtras();
    
            String message = extras != null ? extras.getString("com.parse.Data")
                    : "";
            String channel = extras.getString("com.parse.Channel");
    
    
            JSONObject jObject = null;
            try {
                if (message != null && !message.equals("")) {
                    jObject = new JSONObject(message);
    
                    from = jObject.getString("from");
                   messageText = jObject.getString("a");
                   type  = jObject.getString("t");
    
                       }
                   }
            catch (JSONException e) {
                e.printStackTrace();
            }
    
             ActivityManager am =(ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
              List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
               ComponentName componentInfo = taskInfo.get(0).topActivity;
    
               if(componentInfo.getPackageName().equalsIgnoreCase("com.pappchat"))
             {
                   if (type.equals("Papp App")) {
                   if(taskInfo.get(0).topActivity.getClassName().equals("com.pappchat.NChatActivity"))
                  {
                      LoadDataTwo.getfriendInfo(from,context);
                      Message msg = handler.obtainMessage();
                        Bundle b = new Bundle();
                        b.putString("message", messageText);
                        b.putString("from", from);
    
                        msg.setData(b);
    
                        if(handler.sendMessage(msg)){
                            Log.d(TAG, "send message successfully");
                        }else{
                            Log.d(TAG, "unable to send message.");
                        }
                   }
                   else
                      {
                           notify(context,intent,jObject);
                         }
                    }
    

    这里我在名为 notify(context,intent,jObject) 的方法中制作了自定义通知,您可以在此方法中制作警报对话框(当应用程序处于后台时)。

    看看这个链接,可能对你有用:automatically-open-an-activity-without-user-action for notification

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多