如果手动通过云消息传递完成了如何实施推送通知单击操作,以及如何在后台以通知方式访问其他数据

时间:2019-03-23 19:47:43

标签: java android firebase push-notification firebase-cloud-messaging

enter image description here

我只想访问在“后台”附加选项中写入的那些自定义数据

在我的数据库中,有与之相关的类别和页面,例如 Sports to Sports.java,Politics to Politics.java

现在要显示通知,我已这样做: 我只希望单击通知应用程序应该阅读类别和索引,并根据该页面的意图

public void setindex(String index,String category){
FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
String uid = current_user.getUid();
DatabaseReference mi = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Last").child(category);
mi.setValue(index).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {

        }
    }
});}

以及如何获取这些标题,消息,类别,onClick索引通知

private void showNotification(String title, String message,String category,String index) {
    setindex(index,catgory);
    switch (category){
        case "Sports":Intent intent=new Intent(getApplicationContext(),Sports.class);break;
        case "Politics":intent=new Intent(getApplicationContext(),Politics.class);break;
        default:break;
    }

    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder b = new NotificationCompat.Builder(getApplicationContext());
    b.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(contentIntent);
    NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1,b.build());
}

我还创建了一个文件FirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    showNotification(remoteMessage.getNotification());
}
private void showNotification(RemoteMessage.Notification notification) {
  PendingIntent pendingIntent=PendingIntent.getActivity(this,0,a,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder=new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher_foreground).setContentTitle(notification.getTitle()).setContentText(notification.getBody()).setAutoCancel(true).setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0,builder.build());

}

以及FirebaseInstaceIdServcie.java

public class MyFirebaseIdService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
    super.onTokenRefresh();
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
}

0 个答案:

没有答案