繁体   English   中英

从parse.com控制台收到推送时显示警报对话框而不是通知

[英]Show alert dialog instead of notification when recieve push from parse.com console

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

首先,我从parse.com下载了示例项目,以了解其工作原理。 当我发送Message或JSON时,我可以在通知栏中接收推送通知。 现在,我想显示一个警报对话框,而不是通知栏中的通知。 我进行了搜索,但是即使在解析网站中也找不到解决方案。

请任何帮助,并在此先感谢。

您好,如果您不使用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)的方法中进行了自定义通知。您可以使用此方法创建警报对话框(当应用程序将在后台运行时)。

看看这个链接,可能对您有用: 自动打开一个没有用户动作的通知活动

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM