PushWoosh - 添加自定义广播接收器
PushWoosh - add custom broadcast receiver
我想要实现的目标:收到 PushWoosh 通知后,检查负载并相应地将用户定向到特定 activity。
我正在关注 PushWoosh FAQ section 中关于
的示例
Using Custom Push Broadcast Receiver in Android
但是,我无法在我的自定义推送中收到任何推送..
这是AndroidManifest.xml
:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:exported="true"
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="${applicationId}" />
</intent-filter>
</receiver>
<receiver android:name=".receivers.PWBroadcastReceiver">
<intent-filter>
<action android:name="${applicationId}.com.arellomobile.android.push.REGISTER_BROAD_CAST_ACTION"/>
</intent-filter>
</receiver>
<service android:name="com.arellomobile.android.push.PushGCMIntentService" />
这是我的自定义广播接收器:
public class PWBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PWBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
// Let Pushwoosh SDK to pre-handling push (Pushwoosh track stats, opens rich pages, etc.).
// It will return Bundle with a push notification data
Bundle pushBundle = PushManager.preHandlePush(context, intent);
if (pushBundle == null)
return;
// Get push bundle as JSON object
JSONObject dataObject = PushManager.bundleToJSON(pushBundle);
// Get default launcher intent for clarity
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launchIntent.addCategory("android.intent.category.LAUNCHER");
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// Put push notifications payload in Intent
launchIntent.putExtras(pushBundle);
launchIntent.putExtra(PushManager.PUSH_RECEIVE_EVENT, dataObject.toString());
// Start activity!
context.startActivity(launchIntent);
// Let Pushwoosh SDK post-handle push (track stats, etc.)
PushManager.postHandlePush(context, intent);
}
}
来自 PWBroadcastReceiver 的远程 intent 过滤器,您不需要它。
我在你的 AndroidManifest.xml
中没有看到任何元标记
您需要在AndroidManifest.xml的元数据标签中添加接收者名称,否则SDK不知道将推送通知路由到哪里。
<receiver android:name=".receivers.PWBroadcastReceiver" />
<meta-data android:name="PW_NOTIFICATION_RECEIVER" android:value=".receivers.PWBroadcastReceiver"/>
UPDATE:
Note that the PW_NOTIFICATION_RECEIVER
expect package + [path to your notification receiver class]
.. If you use an application_id (com.myapp.staging) different from original package name (com.myapp), this may cause problem.. and the fix is to use the original package instead of application id
我想要实现的目标:收到 PushWoosh 通知后,检查负载并相应地将用户定向到特定 activity。
我正在关注 PushWoosh FAQ section 中关于
的示例Using Custom Push Broadcast Receiver in Android
但是,我无法在我的自定义推送中收到任何推送..
这是AndroidManifest.xml
:
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:exported="true"
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="${applicationId}" />
</intent-filter>
</receiver>
<receiver android:name=".receivers.PWBroadcastReceiver">
<intent-filter>
<action android:name="${applicationId}.com.arellomobile.android.push.REGISTER_BROAD_CAST_ACTION"/>
</intent-filter>
</receiver>
<service android:name="com.arellomobile.android.push.PushGCMIntentService" />
这是我的自定义广播接收器:
public class PWBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PWBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null)
return;
// Let Pushwoosh SDK to pre-handling push (Pushwoosh track stats, opens rich pages, etc.).
// It will return Bundle with a push notification data
Bundle pushBundle = PushManager.preHandlePush(context, intent);
if (pushBundle == null)
return;
// Get push bundle as JSON object
JSONObject dataObject = PushManager.bundleToJSON(pushBundle);
// Get default launcher intent for clarity
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
launchIntent.addCategory("android.intent.category.LAUNCHER");
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// Put push notifications payload in Intent
launchIntent.putExtras(pushBundle);
launchIntent.putExtra(PushManager.PUSH_RECEIVE_EVENT, dataObject.toString());
// Start activity!
context.startActivity(launchIntent);
// Let Pushwoosh SDK post-handle push (track stats, etc.)
PushManager.postHandlePush(context, intent);
}
}
来自 PWBroadcastReceiver 的远程 intent 过滤器,您不需要它。
我在你的 AndroidManifest.xml
中没有看到任何元标记
您需要在AndroidManifest.xml的元数据标签中添加接收者名称,否则SDK不知道将推送通知路由到哪里。
<receiver android:name=".receivers.PWBroadcastReceiver" />
<meta-data android:name="PW_NOTIFICATION_RECEIVER" android:value=".receivers.PWBroadcastReceiver"/>
UPDATE: Note that the
PW_NOTIFICATION_RECEIVER
expectpackage + [path to your notification receiver class]
.. If you use an application_id (com.myapp.staging) different from original package name (com.myapp), this may cause problem.. and the fix is to use the original package instead of application id