Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FCM通知未打开预期活动_Android_Firebase_Push Notification - Fatal编程技术网

Android FCM通知未打开预期活动

Android FCM通知未打开预期活动,android,firebase,push-notification,Android,Firebase,Push Notification,我正在尝试获取FCM控制台生成的通知,我正在接收这些通知,但我无法覆盖FirebaseMessagingService的onMessageReceived。不知道我做错了什么 MyFirebaseMessagingService负责处理通知的类: public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "MyFirebaseMs

我正在尝试获取FCM控制台生成的通知,我正在接收这些通知,但我无法覆盖FirebaseMessagingService的
onMessageReceived
。不知道我做错了什么

MyFirebaseMessagingService负责处理通知的类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "FROM:" + remoteMessage.getFrom());

        //Check if the message contains data
        if(remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data: " + remoteMessage.getData());
        }

        //Check if the message contains notification

        if(remoteMessage.getNotification() != null) {
            Log.d(TAG, "Mesage body:" + remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData());
        }
    }

    /**
     * Dispay the notification
     * @param body
     */
    private void sendNotification(String body , Map<String,String> data) {

//        int finalSecId = Integer.parseInt((String) data.get("sec_id"));
//        int sec = Integer.parseInt((String) data.get("sec"));
        Intent intent = new Intent(this, InsuranceActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
        //Set sound of notification
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.login_meter)
                .setContentTitle(getString(R.string.app_name))
                .setContentText((String) data.get("sec_id")+ " "+(String) data.get("sec"))
                .setAutoCancel(true)
                .setSound(notificationSound)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /*ID of notification*/, notifiBuilder.build());
    }
}
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
私有静态最终字符串TAG=“MyFirebaseMsgService”;
@凌驾
public void onCreate(){
super.onCreate();
}
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
Log.d(标记“FROM:”+remoteMessage.getFrom());
//检查消息是否包含数据
如果(remoteMessage.getData().size()>0){
Log.d(标记,“消息数据:”+remoteMessage.getData());
}
//检查消息是否包含通知
if(remoteMessage.getNotification()!=null){
Log.d(标记“Mesage body:”+remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getData());
}
}
/**
*分发通知
*@param body
*/
私有void sendNotification(字符串体、映射数据){
//int finalSecId=Integer.parseInt((字符串)data.get(“sec_id”);
//int sec=Integer.parseInt((字符串)data.get(“sec”);
意向意向=新意向(此,InsuranceActivity.class);
intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent PendingEvent=PendingEvent.getActivity(此,0/*请求代码*/,意图,PendingEvent.FLAG_ONE_SHOT);
//设置通知声音
Uri notificationSound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
NotificationCompat.Builder notifiBuilder=新建NotificationCompat.Builder(此)
.setSmallIcon(R.drawable.login_meter)
.setContentTitle(getString(R.string.app_name))
.setContentText((字符串)数据.get(“sec_id”)+“”+(字符串)数据.get(“sec”))
.setAutoCancel(真)
.setSound(通知声音)
.setContentIntent(挂起内容);
NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
notificationManager.notify(通知的0/*ID*/,notifiBuilder.build());
}
}
和内部应用程序标记

<service android:name=".Fcm.MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".Fcm.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

有两种类型的FCM

通知消息:仅当应用程序位于前台时,发送此消息类型的有效负载才会触发onMessageReceived()

数据消息:仅发送具有此特定消息类型的有效负载会触发onMessageReceived(),无论您的应用程序是否位于前台/后台

参考资料:

试试这个

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);

现在,您有通知类型的通知,这会触发默认通知,只需单击通知即可打开应用程序

因此,您需要将服务器端代码从通知类型更改为数据类型。 并尝试从中获取消息

`remoteMessage.getData()`  not from `remoteMessage.getNotification()`
如果要管理通知的单击,请使用数据类型通知。要了解有关此类型的详细信息,请访问此链接
我在处理FCM的通知时遇到了这个问题

首先,我们必须了解有两种类型的通知

  • 通知-当您的应用程序不在前台时,它将触发并生成通知。如果您单击它,它将打开launcher活动

  • 数据通知-此通知用于解析数据,并在后台和前台接收。因此,您可以基于FCM推送在数据对象中提供的数据构建自定义通知

     Map<String ,String> dataMap = remoteMessage.getData();
    
    Map dataMap=remoteMessage.getData();
    
    在这里,我创建了一个带有键值对的简单映射。现在,我可以在数据对象中接收通知的标题,并制作一个具有自定义意图的简单通知

  • 我个人使用上下文对象来确定应用程序是在前台还是后台。基于此,我决定是显示通知还是只更新数据

    希望这能有所帮助。

    扩展Sudip播客评论和Ratilal Chopda答案 遵循以下步骤:

    步骤1:

    <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name=".SplashActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    
    第三步: 一旦创建您的
    splash活动

     $fields = array(
                'to' => $token,
                'notification' => array(
                    'title' => 'Motors City',
                    'body' => $message,
                    "click_action" => ".AppSplash",
    
                ),
                'data' => array(
                    'sec_id' => $secID,
                    'sec' => $sec,
                    'extra1'=>$extra1,
                    'extra2'=>$extra2
                )
            );
    
            $headers = array(
                'Authorization:key=' . $server_key,
                'Content-Type:application/json'
            );
    
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
      Log.d(TAG,bundle.toString);
    }}
    

    完成后

    您需要从服务器发送数据而不是通知,在消息数据处您必须显示通知实际上数据是从FCM控制台发送的,但我无法在服务中处理它,因为MessageReceived未触发,并且我无法在日志中以及FCM控制台中看到特定标记的结果,使用高级选项并以键值对发送数据。在这之后,您可以在OnMessageReceived()中接收数据,也可以在前台和后台接收通知,但后台通知并不是打开保险活动,而是始终打开主活动。。。是这样吗?我想您已经阅读了代码中已经提到的代码,在您的代码intent.setFlags(intent.FLAG\u ACTIVITY\u CLEAR\u TOP)中;使用AddFlagsID代替setFlags,您将获得解决方案。尝试处理主根活动中的意图写得很好(y)