手柄通知点击推送通知的有效载荷数据 [英] Handle push notification payload data on notification click
问题描述
在通知规定点击:我想处理(执行)IntentReceiver.java类的通知中点击并转发意图
Requirement on notification clicked: I want to handle(execute) IntentReceiver.java class on click of the notification and then forward intent.
IntentReceiver.java(BroadcastReceiver的子类)---> Notification.java(活动)--->主仪表板(活动)
1)。在我的申请,我有一个单独的类IntentReceiver.java,这是广播接收器的子类。
1.) In my application, I have a separate class "IntentReceiver.java", which is the subclass of "BroadcastReceiver".
2)。在那之后,我的IntentReceiver.java类中,我切换到它们没有了Notification.java级
布局屏幕,操纵一些数据,并切换到主仪表板。
2.) After that, inside my "IntentReceiver.java" class, I switch to the "Notification.java" class which have no layout screen, manipulate some data and switch to the main dashboard.
3)。在此主仪表板,我会处理代表不同钥匙的不同的对话中获得(通过putExtra())
在操作之后,从通知的.java级主仪表板。
3.) On this main dashboard, I will handle different dialogue on behalf of the different keys received(via putExtra()) on main dashboard from "Notification .java" class after a manipulation.
code为IntentReceiver.java类:这是一个单独的类来处理每个通知
Code for IntentReceiver.java class : This is a separate class to handle each of the notification.
public class IntentReceiver extends BroadcastReceiver {
Context ctx;
private static String PUSH_KEY_ALERT = "alert";
@Override
public void onReceive(Context context, Intent intent) {
this.ctx = context;
String alert = intent.getStringExtra(PUSH_KEY_ALERT);
Bundle extras = getResultExtras(true);
extras.putInt(PushIOManager.PUSH_STATUS, PushIOManager.PUSH_HANDLED_NOTIFICATION);
setResultExtras(extras);
}
}
清单配置:
<receiver android:name="com.DxS.android.push.IntentReceiver" > </receiver>
<activity android:name=".Notification">
<action android:name="com.DxS.android.NOTIFICATIONPRESSED" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
<activity android:name=".dashboard"> </activity>
这是我要求流,可以请提供做一个最好的办法。
在此先感谢...
This is the flow of my requirement, Can you please provide a best way to do that. Thanks in advance...
推荐答案
首先,你的的onReceive
方法应该检查错误。
下面code将显示一个通知,并在通知被窃听启动通知
活动。我不知道什么是通知活动的目的,如果它没有布局。也许它没有这样的活动,并攻通知应该启动仪表
活动直接。
First of all, your onReceive
method should check for errors.
The following code will display a notification and start your Notification
activity when the notification is tapped. I'm not sure what's the purpose of the Notification activity if it has no layout. Perhaps it doesn't have to be an activity, and tapping the notification should start the dashboard
activity directly.
public class IntentReceiver extends BroadcastReceiver {
static final String TAG = "IntentReceiver";
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
Log.i(TAG, "PUSH RECEIVED WITH ERROR: " + intent.getExtras().toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
Log.i(TAG, "DELETED PUSH MESSAGE: " + intent.getExtras().toString());
else
{
Log.i(TAG, "Received PUSH: " + intent.getExtras().toString());
postNotification(intent.getExtras());
}
setResultCode(Activity.RESULT_OK);
}
// post GCM message to notification center.
private void postNotification(Bundle data) {
String msg = data.getString("alert");
Log.i(TAG, "message: " + msg);
Intent intent = new Intent(ctx, Notification.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
.setContentTitle("Your Title")
.setContentText(msg)
.setTicker(msg)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
这篇关于手柄通知点击推送通知的有效载荷数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!