Android自定义类扩展ParsePushBroadcastReceiver通知

时间:2015-02-18 17:23:49

标签: android parse-platform push

我正在尝试通过扩展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类中的操作是DELETE还是OPEN。 onReceive方法。我不知道这是否是一个很好的解决方案,但至少现在会显示通知。请告诉我是否有另一种解决方案,我可以使用自己的课程进行RECEIVE操作。

2 个答案:

答案 0 :(得分:2)

getNotification覆盖ParsePushBroadcastReceiver方法以显示通知:

@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();
 }

答案 1 :(得分:1)

好的,所以我将清单改回了

<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):< / p>

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);
}

我想这回答了我的问题。

相关问题