Android 自定义 class 扩展 ParsePushBroadcastReceiver 通知
Android custom class extending ParsePushBroadcastReceiver notification
我正在尝试通过扩展 ParsePushBroadcastReceiver class 来处理我的推送通知来使用 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>
这是自定义推送接收器class。
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 class' onReceive
方法中检查操作是删除还是打开。我不知道这是否是一个好的解决方案,但至少通知现在出现了。请告诉我是否有其他解决方案,我可以使用我自己的 class 进行 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
class 中添加了自定义方法来生成通知参数 (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);
}
我想这回答了我的问题。
我正在尝试通过扩展 ParsePushBroadcastReceiver class 来处理我的推送通知来使用 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>
这是自定义推送接收器class。
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 class' onReceive
方法中检查操作是删除还是打开。我不知道这是否是一个好的解决方案,但至少通知现在出现了。请告诉我是否有其他解决方案,我可以使用我自己的 class 进行 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
class 中添加了自定义方法来生成通知参数 (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);
}
我想这回答了我的问题。