繁体   English   中英

Android 应用程序未使用 firebase 启动意图

[英]Android app does not start intent with firebase

我有一个使用 firebase 来显示通知的应用程序,但我有一个活动,而不是显示通知,它应该启动一个活动意图。

错误是在 android 9 版本中

这是我的代码:

FirebaseMessagingServiceApp:

public void onMessageReceived(RemoteMessage remoteMessage) {
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        int value = Integer.parseInt(remoteMessage.getData().get("value"));
        showNotification(value);
    }

}

private void showNotification(int value) {
    Intent intent = new Intent(this, NewClassMessage.class);
    intent.putExtra("value", value);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

这是我的清单

<activity
    android:name=".NewClassMessage"
    android:label="Detail"
    android:windowSoftInputMode="stateAlwaysHidden"
    android:screenOrientation="portrait" />

我究竟做错了什么? 在较低版本中,活动开始时错误在 android 9

提前致谢

尝试使用 PendingIntent 从通知中打开

Intent notificationIntent = new Intent(this, NewClassMessage.class);
    notificationIntent.putExtra("value", value);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
    uniqueInt, notificationIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);

运行 Android 6 及更高版本的设备不会显示通知,因为您需要为通知管理器设置一个通知渠道。

尝试这个

public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {

        // TODO(developer): Handle FCM messages here.

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

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
//            Map<String,String> data=remoteMessage.getData();
//            Toast.makeText(getApplicationContext(),data.get("Firefy"),Toast.LENGTH_LONG);
            Map<String,String> data=remoteMessage.getData();
            showNotification(data);

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            //Toast.makeText(this, "Message Notification Body: " + remoteMessage.getNotification().getBody(),Toast.LENGTH_LONG);
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }

    public void showNotification(Map<String,String> dataReceived) {
        NotificationManager mNotificationManager;

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(getApplicationContext(), "notify_001");
        Intent ii = new Intent(getApplicationContext(), SignUpActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, ii, 0);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);



        Bitmap bitmap = getBitmapfromUrl(dataReceived.get("image"));

        mBuilder.setContentIntent(pendingIntent)
                .setPriority(Notification.PRIORITY_MAX)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setLargeIcon(bitmap)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(bitmap)
                        .bigLargeIcon(null)
                        .setBigContentTitle("hahaha")
                        .setSummaryText("we are done \n"+dataReceived.get("account")+"="+dataReceived.get("balance")))
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setOnlyAlertOnce(true);

        mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// === Removed some obsoletes
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "hahah Channel";
            NotificationChannel channel = new NotificationChannel(channelId,"hahaha notifications",NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("telja notifications keeps  you in the loop.");
            mNotificationManager.createNotificationChannel(channel);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            mNotificationManager.createNotificationChannel(channel);
            mBuilder.setChannelId(channelId);
        }

        mNotificationManager.notify(0, mBuilder.build());

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM