gpt4 book ai didi

android - 如何在 Android 的应用程序中打开/关闭 GCM 推送通知的设置功能?

转载 作者:行者123 更新时间:2023-11-30 02:41:29 24 4
gpt4 key购买 nike

我正在集成 GCM 通知,这工作正常。现在我正在添加 开/关 通知的切换按钮。我停止/启动服务但没有成功。请建议如何管理服务和接收者?我的代码在下面。

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
super("123456789123");
}

/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
ZunaCard.displayMessage(context,
"Your device registred with GCM");
ZunaCard.saveDeviceId(registrationId);
}

/**
* Method called on device unRegistred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
ZunaCard.displayMessage(context,
"From GCM: device successfully unregistered!");
}

/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String msgNotif = intent.getExtras().getString("message");
if (!TextUtils.isEmpty(msgNotif)) {
System.out.println("Called onMessage>>>>>>>>>>>>>>>>>"+msgNotif);
generateNotification(context, ""+msgNotif);
}



}

/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
String message = "From GCM: server deleted %1$d pending messages!";
ZunaCard.displayMessage(context, message);
// notifies user
generateNotification(context, message);
}

/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
ZunaCard.displayMessage(context, "From GCM: error (%1$s). "
+ errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
ZunaCard.displayMessage(context,
"From GCM: recoverable error (%1$s). " + errorId);
return super.onRecoverableError(context, errorId);
}

/**
* Issues a notification to inform the user that server has sent a message.
*/
private static void generateNotification(Context context, String message) {

System.out.println("Called generateNotification>>>>>>>>>>>>>"+message);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = new Notification(icon, message, when);

String title = context.getString(R.string.app_name);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)

.setStyle(
new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message);

Intent notificationIntent = new Intent(context,
SplashActivity.class);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

mBuilder.setContentIntent(intent);

Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);

}}

和接收者代码:

public class GCMReceiver extends GCMBroadcastReceiver {
@Override
protected String getGCMIntentServiceClassName(Context context) {
return context.getString(R.string.gcm_service_class);
}}

最佳答案

有两种实现方式。


  1. you can Unregistered key from GCM.
  2. you can remove device token from server via using service in case of OFF Notification from application.

跳这会给出想法。

关于android - 如何在 Android 的应用程序中打开/关闭 GCM 推送通知的设置功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683785/

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