Android-在应用程序加载通知时会从通知中打开应用程序,但后台不会打开

时间:2019-04-05 16:05:58

标签: java android notifications

我的目标


我正在使用PHP脚本构建应用程序,以通过Firebase向Android应用程序发送通知。最终目标是将数据包包括在通知中,以便您在打开通知时可以读取数据类型,并在打开应用程序时向您显示正确的信息或页面。

我将通知工作到接收并显示在手机顶部栏,通知中心的位置,并在应用程序本身中作为弹出窗口显示。如果单击通知,它将打开Initialize.class的意图以启动应用程序。

我想检查应用程序是在后台运行,在前台运行还是根本不运行。在背景或前景中时。我们将跨整个包发送数据包数据,并重新加载整个应用程序(登录任务将根据初始化意图进行处理。)

如果应用未运行,我们将启动登录任务,然后根据通知处理事件。这是在很多我想关注的应用程序上进行通知的非常标准的方法。


当前正在发生什么?


现在,通知将使用NotificationCompatNotificationManager进行显示,我将在下一部分中发布代码,但是打开应用程序时的行为并不一致。目前,我只是在每次单击通知时都重新加载应用程序,同时尝试使数据正确传递,但稍后会进行更改。目前,当在应用程序处于前台状态时单击通知时,它将成功地将intent.putExtra函数传递给Initialize.class。运行Initialize.class后,我用intent.hasExtra(CHECK_FOR_IT)进行了检查,并在控制台中打印了一条日志,指出是否成功。当应用程序处于前台时,它会通过并说它已设置。然而!如果我在应用程序处于后台运行或根本不运行时单击通知,那么它说没有数据传递且失败了。哇!


奥尔代码


首先,我们使用以下代码块从MyFirebaseMessagingService(扩展FirebaseMessagingService)中获取数据

public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "The notification had a payload attached to it.");
    }
    if (remoteMessage.getNotification() != null) {
        String title = remoteMessage.getNotification().getTitle();
        String body = remoteMessage.getNotification().getBody();
        MyNotificationManager.getInstance(getApplicationContext()).displayNotification(title, body);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel mNotificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            mNotificationChannel.setDescription(CHANNEL_DESCRIPTION);
            mNotificationChannel.enableLights(true);
            mNotificationChannel.setLightColor(Color.RED);
            mNotificationChannel.enableVibration(true);
            mNotificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            Objects.requireNonNull(mNotificationManager).createNotificationChannel(mNotificationChannel);
        }
    }
}

然后将其传递给我们的自定义NotificationManager类。我们将添加Intent并在其中添加一个自定义字符串,而不是来自通知 YET 的数据,以确保该字符串正确无误,并且完全不为空: >

// Set the context here
private MyNotificationManager(Context context) {
    mCtx = context;
}
void displayNotification(String title, String body) {
    long[] pattern = {0, 300, 0};
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_stat_notification)
            .setContentTitle(title)
            .setColor(Color.parseColor("#991e37"))
            .setAutoCancel(true)
            .setSound(alarmSound)
            .setLights(Color.BLUE, 500, 500)
            .setVibrate(pattern)
            .setContentText(body)
            .setChannelId(CHANNEL_ID);
    Intent intent = new Intent(mCtx, Initialize.class);
    intent.putExtra("NOTIFICATION_PACKET", "THIS IS NOTIFICATION DATA");
    intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
    mBuilder.setContentIntent(pendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
    if (mNotificationManager != null) {
        Notification notification = mBuilder.build();
        notification.sound = alarmSound;
        notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(1, notification);
    }
}

最后,在Initialize.class方法中的onCreate上,我们正在使用以下代码块来测试是否存在多余的代码并进行一些控制台调试。

Intent intent = getIntent();
if (intent.hasExtra("NOTIFICATION_PACKET")) {
    Log.e(TAG, "The app was started from a notification, we can process the packet now.");
    Log.e(TAG, getIntent().getStringExtra("NOTIFICATION_PACKET"));
} else {
    Log.e(TAG, "There is no extra intent being passed on the NOTIFICATION_PACKET channel.");
}

这是显示通知的相关代码。目前,就像我在上面说过的那样,在应用程序上单击通知时,您可以在应用程序上正常运行,但是从通知启动应用程序或从后台启动应用程序将不起作用。我在这里缺少简单的东西吗?谢谢。

0 个答案:

没有答案