android 接收推送后运行程序,android – 应用程序运行时推送通知

我正在实施推送通知,到目前为止它工作正常.我设法获得推送通知,当我点击它能够开始活动时.

但如果应用程序已在运行,我不想通知用户有关通知的信息.这就是我计划这样做的方法……但不确定这是否正确

Intent actIntent = new Intent(this, MainActivity.class);

actIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pIntent = PendingIntent.getActivity(this, 0, actIntent, 0);

if (!isActivityRunning())

mNotificationManager.notify(0, notification);

public boolean isActivityRunning(Context ctx) {

ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);

List tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

for (RunningTaskInfo task : tasks) {

if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))

return true;

}

return false;

}

isActivityRunning函数将基本检查MainActivity是否正在运行.如果它处于运行状态,则不会显示通知,并将信息传递给活动本身以更新UI.如果点击通知未运行活动,则会打开MainActivity.

这是实现这一目标的正确方法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中的推送权限是指应用程序需要获得用户授权才能发送推送通知。在 Android 系统中,推送权限属于一项敏感权限,需要用户明确同意才能使用。以下是获取推送权限的一般步骤: 1. 在 AndroidManifest.xml 文件中声明推送服务: ```xml <service android:name=".MyFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> ``` 2. 创建一个继承自 FirebaseMessagingService 的服务类,用于处理接收到的推送消息: ```java public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { // 处理接收到的推送消息 } } ``` 3. 在应用中集成 Firebase Cloud Messaging (FCM) 或其他推送服务提供商的 SDK,以便发送和接收推送消息。 4. 在应用运行时,请求推送权限: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建通知通道 NotificationChannel channel = new NotificationChannel( channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT ); // 设置通知渠道的描述 channel.setDescription(channelDescription); // 在系统上注册通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 5. 向用户请求推送权限: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 检查是否已经获得推送权限 NotificationManager notificationManager = getSystemService(NotificationManager.class); NotificationChannel channel = notificationManager.getNotificationChannel(channelId); if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { // 引导用户去应用设置页面开启推送权限 Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()); intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId()); startActivity(intent); } } ``` 请注意,以上步骤是一般性的流程,具体实现可能会因推送服务提供商和所用技术而有所不同。您需要参考相关的推送服务文档和 Android 官方文档进行具体操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值