【问题标题】:how to open different activities when receiving notification from parse从解析接收通知时如何打开不同的活动
【发布时间】:2015-07-05 12:59:38
【问题描述】:
我的应用中有不同的类别,例如 新闻、天气、体育和 技术,我使用 parse 来接收通知,但我希望自定义通知一旦收到关于新闻的通知,它应该打开新闻活动,如果收到关于技术的通知,它应该打开技术活动。
我不想要自定义推送接收器,我尝试了很多例子,但我无法得到它。任何想法提前谢谢
【问题讨论】:
标签:
android-activity
parse-platform
push-notification
broadcastreceiver
google-cloud-messaging
【解决方案1】:
首先,您需要创建一个扩展 ParsePushBroadcastReceiver 的自定义类。
在那个类中你重写 onPushOpen 方法:
public class ParsePushCustomReceiver extends ParsePushBroadcastReceiver {
protected static String pushTitle="";
@Override
protected void onPushOpen(Context context, Intent intent) {
super.onPushOpen(context, intent);
pushTitle="";
try {
Bundle extras = intent.getExtras();
if (extras != null) {
String jsonData = extras.getString("com.parse.Data");
JSONObject json;
json = new JSONObject(jsonData);
pushTitle = json.getString("title");
String pushContent = json.getString("alert");
//intent to your activity
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
现在当你有了推送的标题和内容后,你就可以直接进入你想去的活动了……
您需要在 AndroidManifest.xml 中将自定义 ParsePushBroadcastReceiver 定义为接收者:
<!-- Parse PushService-->
<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.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.appname.appname.ParsePushCustomReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.appname.appname" />
</intent-filter>
</receiver>