微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用远程消息启动android应用程序?

如何解决如何使用远程消息启动android应用程序?

我是android中远程消息的新手,当从服务器接收消息时执行此代码。这个想法是在消息到达应用程序时启动应用程序,直到sdk 26都可以正常工作,但是在新的sdk版本中,消息不会启动应用程序。我还需要什么呢?一些权限,其他标志?

public void onMessageReceived(RemoteMessage message) {
    try {
        
        Uri uriBeep = Uri.parse("someExample" + R.raw.win);
        notificationmanager manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",notificationmanager.IMPORTANCE_HIGH);
            channel.enableLights(true);
            channel.setLightColor(Color.GRAY);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            channel.setSound(uriBeep,audioAttributes);
            channel.enableVibration(true);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }

        }


        Intent intent = new Intent(getApplicationContext(),MainActivity.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(uriBeep)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(message.getData().get("message"))
                .setAutoCancel(true)
                .setPriority(1)
                .setChannelId(NOTIFICATION_CHANNEL_ID)
                .setVibrate(VIBRATE_PATTERN)
                .setContentIntent(pendingIntent);

        if (manager != null) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            builder.setChannelId(NOTIFICATION_CHANNEL_ID);
            manager.notify(0,builder.build());
        }

    } catch (Exception e) {
        e.printstacktrace();
    }
}

解决方法

从Android 10(API级别29)开始,存在一些限制,这些限制会阻止后台代码启动活动。参见https://developer.android.com/guide/components/activities/background-starts

这个想法是,推送通知和其他异步事件不应干扰用户当前正在执行的操作。可接受的方法是显示Notification,然后用户可以在需要时打开应用程序。这会将控制权交给用户。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?