Android Implementing chat via push notification using parse.com api

I am making a chat application. In the app, I have to chat with push notifications using parse andorid sdk. I was able to generate push notifications between different users. But unable to receive push and add my data as a list. Here is the code of the 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 the code for my usual receiver

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 going wrong for fetching data using the receiver and what to do when I receive a push means any logic or idea to move on. thanks in advance

+2


source to share


2 answers


here is the most explicit 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 get push ...

public class MyCustomReceiver extends BroadcastReceiver {

      

String name, 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 request please let me know ....

+1


source


In the above code example, you missed a few things,

Check your AndroidManifest.xml file and add c2dm permissions,

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

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

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



When submitting data, set an 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);

      

0


source







All Articles