gpt4 book ai didi

android - 处理通知点击时的推送通知负载数据

转载 作者:行者123 更新时间:2023-11-29 00:25:50 26 4
gpt4 key购买 nike

点击通知的要求:我想在点击通知时处理(执行)IntentReceiver.java 类,然后转发 Intent 。

IntentReceiver.java(BroadcastReceiver 的子类) ---> Notification.java(activity) ---> Main dashboard(activity)

1.) 在我的应用程序中,我有一个单独的类“IntentReceiver.java”,它是“BroadcastReceiver”的子类。

2.) 之后,在我的“IntentReceiver.java”类中,我切换到“Notification.java”类,它没有 布局屏幕,操作一些数据并切换到主仪表板。

3.) 在这个主仪表板上,我将代表收到的不同 key 处理不同的对话(通过 putExtra()) 在操作后来自“Notification .java”类的主仪表板上。

IntentReceiver.java 类的代码:这是一个单独的类来处理每个通知。

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);

}

}

list 配置:

<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>

这是我的需求流程,能否请您提供一个最好的方法来做到这一点。提前致谢...

最佳答案

首先,您的onReceive 方法应该检查错误。以下代码将显示通知并在通知被点击时启动您的 Notification Activity 。如果没有布局,我不确定通知 Activity 的目的是什么。也许它不一定是一个 Activity ,点击通知应该直接启动 dashboard Activity 。

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());
}
}

关于android - 处理通知点击时的推送通知负载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com