极光推送实现在前台展示弹窗在后台展示通知栏

直奔主题
1.要实现就要拦截消息,判断当前APP是否在前台运行
判断代码

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Handler;

import timber.log.Timber;

/**
 * 作者: Administrator
 * 时间: 2018/10/16 10:24
 * 描述: 主要用于判断APP是否在前台运行
 */
public class ForegroundUtil implements Application.ActivityLifecycleCallbacks {
	
	//单例
	private static ForegroundUtil instance = new ForegroundUtil();

	//用于判断是否程序在前台
	private boolean foreground = false, paused = true;
	//handler用于处理切换activity时的短暂时期可能出现的判断错误
	private Handler handler = new Handler();
	private Runnable check;
	
	public static void init(Application app){
		app.registerActivityLifecycleCallbacks(instance);
    }

    public static ForegroundUtil get(){
        return instance;
    }

    private ForegroundUtil(){}
	
	@Override
	public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

	}

	@Override
	public void onActi
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用极光推送时,可以通过设置通知样式,实现消息弹窗的效果。具体实现方式如下: 1. 在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 2. 在 Application 中初始化极光推送: ```java JPushInterface.setDebugMode(true); // 开启调试模式 JPushInterface.init(this); // 初始化 JPush ``` 3. 在接收到推送消息时,设置通知样式: ```java public class MyJPushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { // 接收到推送消息 String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE); String content = bundle.getString(JPushInterface.EXTRA_ALERT); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notify = new Notification.Builder(context) .setContentTitle(title) .setContentText(content) .setSmallIcon(R.drawable.icon) .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .build(); nm.notify(0, notify); } } } ``` 这样就可以在接收到推送消息时,弹出一个通知样式的弹窗。需要注意的是,为了确保消息能够及时推送到用户设备上,建议在 Application 的 onCreate 方法中初始化极光推送,并且在 AndroidManifest.xml 文件中设置 JPushReceiver、JPushMessageReceiver 和 JPushNotificationReceiver 等相关的广播接收器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值