在通知上选项卡时未触发解析 onPushOpen
Posted
技术标签:
【中文标题】在通知上选项卡时未触发解析 onPushOpen【英文标题】:Parse onPushOpen not triggered when tabbed on notification 【发布时间】:2015-12-08 16:44:55 【问题描述】:我已经实现了CustomNotificationReceiver
并且onReceive
函数工作得很好。但是,当我想通过单击通知来实现更多功能时,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
【问题讨论】:
您尝试添加super.onPushReceive(context, intent);
吗?
是的,但如前所述,登录 onPushReceive 甚至没有打印在控制台中,因此根本不会调用该函数。尽管在某处必须有一个标准的 onOpen,因为我可以通过一个可以正常打开的 Intent
【参考方案1】:
您需要添加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
【讨论】:
【参考方案2】:尽管很久以前就有人问过这个问题,但我一直在为同样的问题苦苦挣扎,经过几个小时的故障排除后,我找到了答案。所以对于任何面临这个问题的人。这是我解决它的方法。
在你的 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 ;-)
它应该可以工作。希望这对将来的任何人都有帮助,祝你们编码愉快!
【讨论】:
以上是关于在通知上选项卡时未触发解析 onPushOpen的主要内容,如果未能解决你的问题,请参考以下文章
切换选项卡时未调用 NSFetchedResultsControllerDelegate 方法