android 使用 parse.com api 通过推送通知实现聊天
Posted
技术标签:
【中文标题】android 使用 parse.com api 通过推送通知实现聊天【英文标题】:android Implement chatting through push notification using parse.com api 【发布时间】:2014-06-23 16:53:03 【问题描述】:我正在做一个聊天应用程序。在应用程序中,我必须在使用 parse 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();
请告诉我使用接收器接收数据的错误在哪里,以及当我接收推送时该怎么做意味着任何逻辑或想法可以进一步推进。提前致谢
【问题讨论】:
你能提供一些链接让我使用 parse.com 制作聊天应用吗? 【参考方案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
字符串标题,发件人,味精;
@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();
如有任何进一步的疑问,请告诉我......
【讨论】:
【参考方案2】:在上面的代码示例中,你漏掉了一些东西,
检查您的 AndroidManifest.xml 文件,并添加 c2dm 权限,
权限 android:name="你的 package.permission.C2D_MESSAGE" android:protectionLevel="signature"
使用权限 android:name="com.google.android.c2dm.permission.RECEIVE"
uses-permission android:name="your package.permission.C2D_MESSAGE"
发送数据时,在数据中设置动作,
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);
【讨论】:
以上是关于android 使用 parse.com api 通过推送通知实现聊天的主要内容,如果未能解决你的问题,请参考以下文章
Android 推送通知 - Parse.com API - 不工作?
如何使用 REST API 从 Parse.com 下载文件?
如何使用 parse.com 将 Android 设备注册到推送通道
Android - 使用我自己的数据使用 parse.com 制作聊天应用程序