![](/img/trans.png)
[英]customize BigTextStyle notification with ParsePushBroadcastReceiver
[英]Android custom class extending ParsePushBroadcastReceiver notification
我試圖通過擴展ParsePushBroadcastReceiver類來處理我的推送通知來使用Parse。 我遇到的問題是,當我從Parse儀表板進行測試推送時,通知不會顯示。 但我知道我正在接受推送,因為在我的onReceive
功能上,我記錄了消息並顯示出來。 但是通知根本沒有顯示我在onPushOpen
上工作或只是添加一個檢查以查看操作是否為ACTION_PUSH_OPEN
這是我的應用程序的清單。
<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.app.myapp.PushNotificationReceiver"
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.app.myapp" />
</intent-filter>
</receiver>
這是自定義推送接收器類。
public class PushNotificationReceiver extends ParsePushBroadcastReceiver{
private static final String TAG = "PushNotificationReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Received intent: " + intent.toString());
String action = intent.getAction();
Log.d("TEST", action.toString());
if (action.equals(ParsePushBroadcastReceiver.ACTION_PUSH_RECEIVE)) {
JSONObject extras;
try {
extras = new JSONObject(intent.getStringExtra(ParsePushBroadcastReceiver.KEY_PUSH_DATA));
// I get this on my log like this:
// Received push notification. Alert: A test push from Parse!
Log.i(TAG, "Received push notification. Alert: " + extras.getString("alert"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
我希望這是有道理的。 謝謝!
編輯:
我將清單文件更改為:
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
</intent-filter>
</receiver>
<receiver android:name="com.app.myapp.PushNotificationReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
現在由於默認的ParsePushBroadcastReceiver而顯示推送通知,現在我可以檢查我的自定義Receiver類的onReceive
方法中的操作是DELETE還是OPEN。 我不知道這是否是一個很好的解決方案,但至少現在會顯示通知。 請告訴我是否有另一個解決方案,我可以使用自己的類進行RECEIVE操作。
從ParsePushBroadcastReceiver
覆蓋getNotification
方法以顯示通知:
@Override
protected Notification getNotification(Context context, Intent intent) {
Intent notificationIntent = new Intent(context, TestActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent piIntent=PendingIntent.getActivity(context,0,
notificationIntent,0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(extras.getString("alert")).setAutoCancel(true);
mBuilder.setContentIntent(piIntent);
return mBuilder.build();
}
好的,所以我把清單改回來了
<receiver android:name="com.app.myapp.PushNotificationReceiver"
android:permission="com.google.android.c2dm.permission.SEND"
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>
我添加了一個自定義方法來生成我的PushNotificationReceiver
類中的通知,我在我的onReceive
方法中調用該方法,該方法將JSON有效負載作為第二個參數( ParsePushBroadcastReceiver.KEY_PUSH_DATA ):
private void generate_notification(Context context, JSONObject json){
try {
notification = json.getString("alert");
} catch (JSONException e) {
e.printStackTrace();
}
Intent i = new Intent(context, NotificationHandler.class);
PendingIntent pintent = PendingIntent.getActivity(context, 0, i, 0);
Notification notif = new NotificationCompat.Builder(context)
.setContentTitle("My App")
.setContentText(notification)
.setSmallIcon(R.drawable.ic_logo)
.setContentIntent(pintent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notif);
}
我想這會回答我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.