0

I'm trying to add push notifications to my android application.

First I downloaded the example project from parse.com to see how it works. When I send Message or JSON I can receive push notifications in the notifications bar. Now I want to show an alert dialog instead of a notification in the notifications bar. I've searched but I didn't find a solution even in parse website.

Any help please and thanks in advance.

JB.
  • 40,344
  • 12
  • 79
  • 106
TEST USER
  • 41
  • 1
  • 6

1 Answers1

0

Hello if you will not use "title" and "alert" key in your jsonObject then it will not create the by default notification in notification bar.I was able to made a custom notification instead of by default.

You can see my code and change the custom notification code from the alert dialog code.Take a look:

Code to send notification:

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

Code to receive notification :

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

Here I have made custom notification in the method named notify(context,intent,jObject) You can make alert dialog in this method(when the app will be in background).

Take a look on this link ,may be useful for you : automatically-open-an-activity-without-user-action for notification

Community
  • 1
  • 1
userAndroid
  • 586
  • 2
  • 9
  • 26