Firebase通知,Android应用程序立即启动和关闭

时间:2016-10-10 08:18:21

标签: android firebase firebase-notifications

当我从Firebase控制台发送Firebase通知时,它会启动我的启动画面,并且通知图标显示在显示屏顶部。我不知道是什么导致这种行为。有任何想法吗?

这是我的MyFirebaseMessagingService.class

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.Novatech.paybox.Activities.MainActivity;
import com.Novatech.paybox.Activities.NewsActivity;
import com.Novatech.paybox.Classes.News;
import com.Novatech.paybox.R;
import com.Novatech.paybox.SQLite.DBHandler;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.List;

public class MyFirebaseMessegingService extends FirebaseMessagingService {

    String Title;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent intent  = new Intent(this, NewsActivity.class);
        if(remoteMessage.getData().size() > 0){
            String message = remoteMessage.getData().get("message");
            String title = remoteMessage.getData().get("title");
            Title = title;
            System.out.println("TitleFrom MEssageingService::"+title);
            Bundle bundle = new Bundle();
            bundle.putString("message",message);
            intent.putExtras(bundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setContentIntent(pendingIntent);
        notificationBuilder.setContentTitle(Title);
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(getNotificationIcon());
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
    private int getNotificationIcon(){
        boolean userWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
        return userWhiteIcon ? R.drawable.paybox_logo_transparant : R.drawable.paybox_logo;
    }
}

这是MyFirebaseInstanceIdService

import android.content.Context; import android.content.SharedPreferences; import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceIdService;


public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
    private static final String TAG = "REG_TOKEN";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);


       // Log.d(REG_TOKEN,recent_token);
       // System.out.println("REG_TOKEN::"+recent_token);
    } }

这是我的主要活动,如果他/她点击通知,我会将用户重定向到另一个活动。

String notificationMessegaBody="a";

        if(getIntent().getExtras() != null){
            notificationMessegaBody = getIntent().getExtras().getString("id");
            System.out.println("MessageBody FromLoginChooserActivity::" + notificationMessegaBody);
            //insert news in SQLite Database
            if(notificationMessegaBody!="a"){
                if(!(notificationMessegaBody == null)){
                    Intent intent = new Intent(LoginChooserActivity.this, NewsActivity.class);
                    startActivity(intent);
                }
            }
        }else{
            System.out.println("extras are empty");
        }

0 个答案:

没有答案