如何在Android应用程序中启用/关闭GCM推送通知的设置功能? [英] how to put the settings functionality for GCM Push Notification on/off inside the app in android?
本文介绍了如何在Android应用程序中启用/关闭GCM推送通知的设置功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在整合GCM通知,这工作正常。现在我添加开/关通知的切换按钮。我停止/启动服务,但没有成功。请建议如何管理服务和接收器?我的代码如下。
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = GCMIntentService;
public GCMIntentService(){
super(123456789123);
$ b $ **
*在设备上注册的方法已注册
** /
@Override
protected void onRegistered(Context context, String registrationId){
Log.i(TAG,注册的设备:regId =+ registrationId);
ZunaCard.displayMessage(上下文,
您的设备注册GCM);
ZunaCard.saveDeviceId(registrationId);
$ b $ **
*在设备上调用的方法unRegistred
* * /
@Override
protected void onUnregistered(Context context,字符串registrationId){
Log.i(标签,设备未注册);
ZunaCard.displayMessage(context,
From GCM:device successfully unregistered!);
$ b / **
*调用方法接收新消息
* * /
@Override
protected void onMessage(Context意图意图){
Log.i(TAG,收到的信息);
String msgNotif = intent.getExtras()。getString(message);
if(!TextUtils.isEmpty(msgNotif)){
System.out.println(Called onMessage>>>>>>>>>>>> ;>>>>>中+ msgNotif);
generateNotification(context,+ msgNotif);
}
}
/ **
*接收删除信息的方法
* * /
@Override
protected void onDeletedMessages(Context context,int total){
Log.i(TAG,Received deleted messages notification);
String message =从GCM:服务器删除%1 $ d待处理的邮件!;
ZunaCard.displayMessage(context,message);
//通知用户
generateNotification(context,message);
$ b $ ** b $ b *错误
* * /
@Override
public void onError(Context context,String errorId){
Log.i(TAG,Received error:+ errorId);
ZunaCard.displayMessage(context,From GCM:error(%1 $ s)。
+ errorId);
$ b @Override
protected boolean onRecoverableError(Context context,String errorId){
// log message
Log.i(TAG,Received可恢复的错误:+ errorId);
ZunaCard.displayMessage(context,
From GCM:recoverable error(%1 $ s)。+ errorId);
返回super.onRecoverableError(context,errorId);
}
/ **
*发出通知以通知用户服务器已发送消息。
* /
private static void generateNotification(Context context,String message){
System.out.println(调用generateNotification>>>>>> >>>>>>>中+消息);
NotificationManager notificationManager =(NotificationManager)context
.getSystemService(Context.NOTIFICATION_SERVICE);
//通知通知=新通知(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);
通知通知= 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);
}}
解决方案
我会用这个注销GCM服务和Receiver:
public static void unregisterGCMComponents(Context context)
{
int flag = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
ComponentName component1 = new ComponentName(context,GCMIntentService.class);
context.getPackageManager()。setComponentEnabledSetting(component1,flag,PackageManager.DONT_KILL_APP);
ComponentName component2 = new ComponentName(context,GCMBroadcastReceiver.class);
context.getPackageManager()。setComponentEnabledSetting(component2,flag,PackageManager.DONT_KILL_APP);
$ b因此,你可以从你的启动器类或应用程序类中调用此方法像这样:
if(!notification_flag)// push设置为ON
unregisterGCMComponents(this);
I am integrating GCM notification, this is working fine. Now i am adding toggle button of on/off notification.I stops/start service but no success. Please suggest how to manage service and receiver? my code is below.
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);
}}
and receiver code:
public class GCMReceiver extends GCMBroadcastReceiver {
@Override
protected String getGCMIntentServiceClassName(Context context) {
return context.getString(R.string.gcm_service_class);
}}
解决方案 I would use this to Unregister the GCM service and Receiver:
public static void unregisterGCMComponents(Context context)
{
int flag = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
ComponentName component1 = new ComponentName(context, GCMIntentService.class);
context.getPackageManager().setComponentEnabledSetting(component1, flag, PackageManager.DONT_KILL_APP);
ComponentName component2 = new ComponentName(context, GCMBroadcastReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component2, flag, PackageManager.DONT_KILL_APP);
}
So, you can call this method from your launcher class or "Application" class like this :
if(!notification_flag) //push is set to ON
unregisterGCMComponents(this);
这篇关于如何在Android应用程序中启用/关闭GCM推送通知的设置功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文