繁体   English   中英

Android自定义类扩展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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM