如何将解析通知添加到我的应用程序?
我有一个应用程序,我想在其中使用解析通知。 我也有一个正确的样本解析推送通知。如何和在哪里可以使用推它呢? 我的解析应用程序拥有这些代码:如何将解析通知添加到我的应用程序?
public class ParseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Add your initialization code here
Parse.initialize(this, " ", " ");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
也:
public class ParseStarterProjectActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// To track statistics around application
ParseAnalytics.trackAppOpened(getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(this, ParseStarterProjectActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
它工作正常,但我不知道在哪里我可以在这个代码添加到我的主要应用。
现在,由于您没有使用频道或高级定位,您可以将代码放在onCreate方法中,并且推送通知将起作用。通过这种方式,您可以从解析网站发送推送。
如果您想发送推送到设备的子集,您可以使用频道(您可以让用户订阅/取消订阅并发送推送)或高级定位。通过这些方式,无论何时发生事件,您都可以从解析网站或应用程序本身发送推送到您想要的设备。
你需要写一个自定义的接收器,它可以读取你的推送通知,并因此采取相应的行动。
您可以按照以下步骤在您的应用程序中启用Parse通知解析。我假设你已经在你的项目中包含了相关的jar文件(Parse-1.7.1.jar在我的案例中)。
步骤-1:在应用程序类的onCreate()函数,申报 Parse.initialize(此,Constants.PUSH_NOTIFICATION_APPLICATION_ID,Constants.PUSH_NOTIFICATION_CLIENT_ID);
用您的凭证替换PUSH_NOTIFICATION_APPLICATION_ID和PUSH_NOTIFICATION_CLIENT_ID。
第2步:在您的Manifest文件中包含以下行。
<service android:name="com.parse.PushService" />
<receiver
android:name="path to class.CustomReceiver"
android:exported="false"
android:icon="@drawable/notification_icon"
android:logo="@drawable/notification_icon" >
<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" />
<category android:name="your package name" />
</intent-filter>
</receiver>
步骤3:创建CustomReceiver类,如:
public class CustomReceiver extends ParsePushBroadcastReceiver {
private final String TAG = "ParseNotification";
private String msg = "";
private String[] split = new String[2];
public static SharedPreferences settingsUserProfile;
private String promoOfferUrl = null;
public void onPushOpen(Context context, Intent intent) {
try {
String action = intent.getAction();
String channel = intent.getExtras().getString("com.parse.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action: " + action + " on channel: " + channel + " with:");
Iterator itr = json.keys();
while(itr.hasNext()) {
String key = (String) itr.next();
Log.d(TAG, "..." + key + " => " + json.getString(key));
if(key.equals("move")) {
msg = json.getString(key);
}
}
} catch (JSONException e) {
Log.d(TAG, "JSON Exception: " + e.getMessage());
}
if(msg.equals("1")) {
// move to Activity X
} else {
// move to Activity Y
}
}
STEP-4:写你的推送通知,如:
{
"alert": "Hello World",
"title": "bye bye",
"move": "1"
}
你必须去解析用户界面并在“编写消息”部分中,选择JSON作为消息类型并粘贴上述示例。在我的情况下,我正在根据“move”参数的值进行不同的活动。您可以相应地设计您的JSON,并因此修改代码以满足您的需求。
希望它能解决您的问题。