2

I am doing a chat application . In the app i have to do chatting with help of push notifications using parse andorid sdk. I am successfull to generate push notications between different users . But not able to recieve push and add their data in list view . Here is code of maifest file

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />





<application
    android:name="example.chat.ChatApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


     <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name="example.chat.LoginActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="example.chat.FriendslIst"
        android:label="Friend list"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="example.chat.RegisterActivity"
        android:label="@string/title_activity_register"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="example.chat.FriendListActivity"
        android:label="@string/title_activity_friend_list"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="example.chat.ChatActivity"
        android:label="@string/title_activity_chat"
        android:screenOrientation="portrait" >
    </activity>

    <service android:name="com.parse.PushService" />

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>



     <receiver android:name="example.chat.MyCustomReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <action android:name="example.chat.UPDATE_STATUS" />
        </intent-filter>
    </receiver>

</application>

and code for my custom reciever

public class MyCustomReceiver extends BroadcastReceiver {
    private static final String TAG = "MyCustomReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {


        Toast.makeText(context, ""+intent, Toast.LENGTH_LONG).show();
        }
}

and from java code I am sending push like this :

ParseQuery<ParseInstallation> query = ParseInstallation.getQuery();
        query.whereEqualTo("device_id", target);
        ParsePush push = new ParsePush();
        push.setQuery(query);
        push.setMessage(message);
        push.setExpirationTimeInterval(86400);
        push.sendInBackground();

Please tell me where I am wrong for recieving data using reciever and what to do when i recive push means any logic or idea to move further . Thanks in advance

1
  • Can you provide me some links to make chat app using parse.com? Commented Jun 7, 2015 at 6:51

2 Answers 2

1

here is the manifiest file.....

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="your package name"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="15"
            android:targetSdkVersion="16" />

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
        <uses-permission android:name="android.permission.VIBRATE" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="your package name.YourActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

            <service android:name="com.parse.PushService" />

            <receiver android:name="com.parse.ParseBroadcastReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
                    <action android:name="android.intent.action.USER_PRESENT" />
                </intent-filter>
            </receiver>
            <receiver android:name="your package name.YourCustomReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.USER_PRESENT" />
                    <action android:name="your package name.UPDATE_STATUS" />
                </intent-filter>
            </receiver>
        </application>

    </manifest>

use this code to receive the push...

public class MyCustomReceiver extends BroadcastReceiver {

String title, from, msg;

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();

        String message = extras != null ? extras.getString("com.parse.Data")
                : "";

        Log.e("message ", " " + message);

        JSONObject jObject;
        try {
            if (message != null && !message.equals("")) {
                jObject = new JSONObject(message);

                from = jObject.getString("from");
                msg = jObject.getString("title");
                title = jObject.getString("msg");



                GCMMessage gcmMessage = new GCMMessage();
                gcmMessage.setMsg_body(msg);
                gcmMessage.setMsg_title(title);
                gcmMessage.setType(0);
                gcmMessage.setDateTime(time);



                DatabaseUtil.insertMessage(context, gcmMessage);
            }

        }

        catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

and here is the code to send push...

    JSONObject obj;
            try {
                obj =new JSONObject();
                obj.put("alert","oman expert ");
                obj.put("action","Your Package name.UPDATE_STATUS");



data.put("from", ParseUser.getCurrentUser().getUsername());         


    obj.put("msg","hi");
    obj.put("title","msg");
                ParsePush push = new ParsePush();
                ParseQuery query = ParseInstallation.getQuery();



                push.setQuery(query);
                push.setData(obj);
                push.sendInBackground(); 
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

for any further query please let me know....

Sign up to request clarification or add additional context in comments.

Comments

0

In the above code example, you have missed out few things,

Check your AndroidManifest.xml file, and add c2dm permissions,

permission android:name="your package.permission.C2D_MESSAGE" android:protectionLevel="signature"

uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"

uses-permission android:name="your package.permission.C2D_MESSAGE"

While sending the data, set the action in your data,

 JSONObject data = new JSONObject();
    try {
        data.put("action", "your package.GOT_MESSAGE");
        data.put("ya", ya);
        data.put("from", ParseUser.getCurrentUser().getUsername());
    }catch (Exception e){
        e.printStackTrace();
        return;
    }


    ParsePush parsePush = new ParsePush();
    parsePush.setData(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.