android 禁止切换应用程序,android – 通过从应用程序设置切换按钮启用或禁用FCM推送通知...

我已在我的应用中通过Firebase实施了推送通知.即使从设置中禁用通知,通知也会到来.

我为Firebase实施的课程是:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

//Displaying data in log

Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

//Calling method to generate notification

String to="";

to = remoteMessage.getData().get("key1");

//when the notification is disabled then also the notification is coming

if(notification_enable) {

sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),to);

}

}

//This method is only generating push notification

//It is same as we did in earlier posts

private void sendNotification(String title,String messageBody,String to) {

Intent intent = new Intent(this, Splash_Activity.class);

intent.putExtra("key1",to);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.noti_icon)

.setContentTitle(title)

.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))

.setContentText(messageBody)

.setAutoCancel(true)

.setColor(this.getResources().getColor(R.color.colorAccent))

.setSound(defaultSoundUri)

.setContentIntent(pendingIntent);

NotificationManager notificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0, notificationBuilder.build());

}

}

public class FirebaseIDService extends FirebaseInstanceIdService {

private static final String TAG = "FirebaseIDService";

@Override

public void onTokenRefresh() {

// Get updated InstanceID token.

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Log.e(TAG, "Refreshed token: " + refreshedToken);

// TODO: Implement this method to send any registration to your app's servers.

sendRegistrationToServer(refreshedToken);

}

/**

* Persist token to third-party servers.

*

* Modify this method to associate the user's FCM InstanceID token with any server-side account

* maintained by your application.

*

* @param token The new token.

*/

private void sendRegistrationToServer(String token) {

// Add custom implementation, as needed.

}

}

并将清单中的类包括在内:

android:name="com.pixelpoint.permission.C2D_MESSAGE"

android:protectionLevel="signature" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值