android使用parse.com api通过推送通知实现聊天

时间:2014-06-23 16:53:04

标签: java android push-notification parse-platform

我正在做一个聊天应用程序。在应用程序中,我必须使用解析andorid sdk与推送通知的帮助聊天。我成功地在不同用户之间生成推送通知。但是无法接收推送并在列表视图中添加其数据。这是maifest文件的代码

<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>

和我的自定义接收器的代码

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

从java代码我发送这样的推送:

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

请告诉我使用接收器接收数据的错误,以及当我接受推送时该怎么做意味着任何逻辑或想法进一步移动。提前致谢

2 个答案:

答案 0 :(得分:1)

这是最明显的文件.....

  <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>

使用此代码接收推送...

public class MyCustomReceiver extends BroadcastReceiver {

字符串标题,来自,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();
        }

    }

}

以下是发送推送的代码......

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

如有任何进一步的问题,请告诉我....

答案 1 :(得分:0)

在上面的代码示例中,您遗漏了一些内容,

检查您的AndroidManifest.xml文件,并添加c2dm权限,

  

权限android:name =&#34;你的package.permission.C2D_MESSAGE&#34;机器人:的ProtectionLevel =&#34;签名&#34;

     

uses-permission android:name =&#34; com.google.android.c2dm.permission.RECEIVE&#34;

     

uses-permission android:name =&#34; your package.permission.C2D_MESSAGE&#34;

发送数据时,请在数据中设置操作

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