通知

时间:2015-12-08 16:44:55

标签: android parse-platform push-notification

我已经实现了CustomNotificationReceiveronReceive功能完全正常。但是当我想通过点击通知来实现一些进一步的功能时,onPushOpen会被忽略。

这是我在AndroidManifest.xml

中加入的Parse部分
<service android:name="com.parse.PushService" />

    <receiver
        android:name="domain.helperClasses.CustomNotificationReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
            <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="domain" />
        </intent-filter>
    </receiver>

我的CustomNotificationReceiver的结构

public class CustomNotificationReceiver extends ParsePushBroadcastReceiver { 
    @Override
    protected void onPushOpen(Context context, Intent intent) {
        Log.d(debug_msg, "OnPushOpen triggered");
        // never gets triggered
    }

    @Override
    public void onPushReceive(Context context, Intent intent) {
        Log.i(debug_msg, "onPushReceive triggered!");
        // works fine
     }

2 个答案:

答案 0 :(得分:0)

您需要添加super.onPushReceive(context, intent);

onPushReceive(context, intent)是受保护的方法,它会将广播发送到onReceive(Context context, Intent intent)

在这种方法中,它有三个动作:删除,打开,接收。

@Override
  public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    switch (intentAction) {
      case ACTION_PUSH_RECEIVE:
        onPushReceive(context, intent);
        break;
      case ACTION_PUSH_DELETE:
        onPushDismiss(context, intent);
        break;
      case ACTION_PUSH_OPEN:
        onPushOpen(context, intent);
        break;
    }
  }

您可以阅读源代码here

请按照文档here

进行操作

答案 1 :(得分:0)

尽管很久以前就有人问过这个问题,但我一直在为同样的问题苦苦挣扎,经过几个小时的故障排除后,我找到了答案。所以对于任何面临这个问题的人。这是我解决它的方法。

在你的 AndroidManifest.xml 中注册一个 BroadcastReceiver

<receiver
   android:name=".YourBroadcastReceiver"
    <intent-filter>
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.OPEN" />
    </intent-filter>
</receiver>

YourBroadcastReceiver.Java

public class YourBroadcastReceiver extends ParsePushBroadcastReceiver {

//This override is not strictly neccesary since it only adds logs for bebugging but for demonstration purposes I'll leave it in.


    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.e(TAG, "Action --> " + intentAction);
        if (intentAction != null) {
            switch (intentAction) {
                case ACTION_PUSH_RECEIVE:
                    Log.e(TAG, "Receive");
                    onPushReceive(context, intent);
                    break;
                case ACTION_PUSH_DELETE:
                    Log.e(TAG, "Delete");
                    onPushDismiss(context, intent);
                    break;
                case ACTION_PUSH_OPEN:
                    Log.e(TAG, "Open");
                    onPushOpen(context, intent);
                    break;
            }
        }
    }
    
    
    //Handle ACTION_PUSH_RECEIVE:

    @Override
    protected void onPushReceive(Context context, Intent intent) {
        //Do stuff with your received data here
        //Whenever you're ready, go prepare stuff for your notification
        
        //Create the intent & pending intent that trigger ACTION_PUSH_DELETE
        Intent intent = new Intent(context, YourBroadcastReceiver.class);
        intent.setAction("com.parse.push.intent.DELETE");
        PendingIntent pIntent;
        pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        
        // Build the notification and add the action
        // Set the pending intent as the delete action!!!
        Notification newMessageNotification = new Notification.Builder(context, "2")
            .setSmallIcon(R.drawable.your_icon)
            .setContentTitle("Your title")
            .setContentText("Your text")
            .setDeleteIntent(pIntent) //<---- This is the important one here
            .setAutoCancel(true)
            .build();
            
        // Issue the notification. Use num to not overwrite existing notifications!
            int num = (int) System.currentTimeMillis();
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            notificationManager.notify(num, newMessageNotification);        
    }
    
    //Handle ACTION_PUSH_DELETE:


    @Override
    protected void onPushDismiss(Context context, Intent intent) {
        Log.e(TAG, "A push has been dismissed");
        //Do something with the dismissed notification, get creative ;-)
    }
    
    
}

有了它它应该可以工作。希望这对未来的任何人都有帮助,祝大家编码愉快!