我在我的项目中使用解析通知。
如果从Parse控制台推送,我已成功收到通知。但是当它从我们的服务器推送时,我无法收到通知。顺便说一句,这个来自服务器的代码也提供给iOS和iOS设备得到了通知。
通知将从我们的服务器推送到Parse,Parse将发送到Android设备。 请提供建议并感谢您的帮助。
我的宣言:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.zukami.apps.dev.blynk.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.zukami.apps.dev.blynk.permission.C2D_MESSAGE" />
<!-- Push notification -->
<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.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.zukami.apps.dev.blynk" />
</intent-filter>
</receiver>
<receiver
android:name="com.zukami.apps.dev.blynk.util.BlynkParseBroadcastReceiver"
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>
<!-- End of push notification -->
我的申请类:
@Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
PushService.setDefaultPushCallback(this,PushActivity.class);
ParsePush.subscribeInBackground("testChannel", new SaveCallback() {
@Override
public void done(com.parse.ParseException e) {
if (e == null) {
Log.e(TAG,
"successfully subscribed to the broadcast channel.");
} else {
Log.e(TAG, "failed to subscribe for push", e);
}
}
});
}
我的活动课程:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_promo_activity);
spinner = (ProgressBar) findViewById(R.id.progressBar1);
fragment = new Fragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container, fragment, PROMO_POPUP);
transaction.commit();
// Track app opens.
ParseAnalytics.trackAppOpened(getIntent());
Bundle extras = getIntent().getExtras();
if (extras != null) {
mData = extras.getString("com.parse.Data");
Log.e(TAG, "DATA ORIGINAL = " + mData);
if (mData != null) {
mData = mData.replace("\\", "").trim();
mData = mData.replace("\"{", "{").trim();
mData = mData.replace("}\"", "}").trim();
Log.e(TAG, "AFTER REPLACE = " + mData);
try {
JSONObject jObject = new JSONObject(mData);
JSONObject alertObj = jObject.getJSONObject("alert");
JSONObject dataObj = alertObj.getJSONObject("Data");
action = dataObj.getString("Action");
JSONObject innerDataObj = dataObj.getJSONObject("Data");
id = innerDataObj.getString("id");
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
}
}
}
我的广播课程:
@Override
public void onPushOpen(Context context, Intent intent) {
Log.e("Push", "Clicked");
Intent i = new Intent(context, PushActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
这是服务器发出推送通知的代码:
var jss = new JavaScriptSerializer();
string endPoint = "https://api.parse.com/1/push";
string applicationID = "APP_ID";
string restAPIKey = "REST_API_KEY";
PushNotificationAction data = new PushNotificationAction();
data.Action = "ViewAction";
data.alert = "Alert action for android";
data.Data = new PushNotificationActionData()
{
id = ActionID
};
var client = new RestClient(endpoint: endPoint,
method: HttpVerb.POST);
client.Headers = new List<string>();
client.Headers.Add(string.Format("X-Parse-Application-Id: {0}", applicationID));
client.Headers.Add(string.Format("X-Parse-REST-API-Key: {0}", restAPIKey));
PushActionNotification push = new PushActionNotification();
push.channels = new List<string>();
push.channels.Add("");
push.data = new PushNotificationActionItem()
{
Data = jss.Serialize(data),
content_available = 1
};
string jsonData = jss.Serialize(push).Replace("content_available", "content-available");
client.PostData = jsonData;
client.ContentType = "application/json";
var json = client.MakeRequest();
response.Status = 1;
return response;
public class PushNotificationAction
{
public string Action { get; set; }
public PushNotificationActionData Data { get; set; }
public string alertMsg { get; set; }
public string alert { get; set; }
}
public class PushNotificationActionData
{
public string id { get; set; }
}
public class PushNotificationActionItem
{
public string Data { get; set; }
public int content_available { get; set; }
}
public class PushActionNotification
{
public List<string> channels { get; set; }
//public string type { get; set; }
public PushNotificationActionItem data { get; set; }
}
答案 0 :(得分:0)
我找到了解决方案!!
问题在于JSON数据。推送到iOS的相同JSON数据无法推送到Android,因此我决定将Android和iOS的推送通知分开。对于Android,模板JSON将如下所示:
{
"data": {
"Action": "ViewAction",
"Data": {
"id": "1432f1ea-33c4-4528-a695-7412a7a7499a"
},
"alertMsg": null
},
"title": "Alert action for android",
"alert": "Alert action for android",
"push_hash": "d265b3500c1089dc0c7c9ace251cfebb"
}
这里有简短说明:
我发布这个答案可能会对以后的某个人有用, 干杯..