/**
 * Android 高优先级通知渠道配置 - 完整示例代码
 * 包名: com.xunpaisoft.social
 */

// ============================================
// 1. Application.java - 创建通知渠道
// ============================================
package com.xunpaisoft.social;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.media.AudioAttributes;
import android.os.Build;

public class MyApplication extends Application {
    
    public static final String CHANNEL_ID_IMPORTANT = "important_messages";
    public static final String CHANNEL_NAME_IMPORTANT = "重要消息";
    
    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannels();
    }
    
    private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = 
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            
            // 检查渠道是否已存在
            if (notificationManager.getNotificationChannel(CHANNEL_ID_IMPORTANT) == null) {
                NotificationChannel importantChannel = new NotificationChannel(
                    CHANNEL_ID_IMPORTANT,
                    CHANNEL_NAME_IMPORTANT,
                    NotificationManager.IMPORTANCE_HIGH  // 高优先级
                );
                
                // 设置渠道描述
                importantChannel.setDescription("重要消息通知，包括即时聊天、语音通话等");
                
                // 启用指示灯
                importantChannel.enableLights(true);
                importantChannel.setLightColor(android.graphics.Color.BLUE);
                
                // 启用振动
                importantChannel.enableVibration(true);
                // 设置振动模式：等待0ms，振动1000ms，暂停500ms，再振动1000ms
                importantChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
                
                // 设置通知声音（使用系统默认通知声音）
                importantChannel.setSound(
                    android.provider.Settings.System.DEFAULT_NOTIFICATION_URI,
                    new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build()
                );
                
                // 在锁屏上显示
                importantChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                
                // 创建渠道
                notificationManager.createNotificationChannel(importantChannel);
            }
        }
    }
}

// ============================================
// 2. PushNotificationHelper.java - 显示通知
// ============================================
package com.xunpaisoft.social;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;

public class PushNotificationHelper {
    
    private static final String CHANNEL_ID = MyApplication.CHANNEL_ID_IMPORTANT;
    
    /**
     * 显示推送通知
     * @param context 上下文
     * @param title 通知标题
     * @param content 通知内容
     */
    public static void showNotification(Context context, String title, String content) {
        NotificationManager notificationManager = 
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        
        // 创建点击意图
        Intent intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        
        PendingIntent pendingIntent = PendingIntent.getActivity(
            context,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT | 
            (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0)
        );
        
        // 构建通知
        Notification.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android 8.0 及以上使用渠道
            builder = new Notification.Builder(context, CHANNEL_ID);
        } else {
            // Android 8.0 以下
            builder = new Notification.Builder(context);
            builder.setPriority(Notification.PRIORITY_HIGH);  // 高优先级
        }
        
        builder.setContentTitle(title)
               .setContentText(content)
               .setSmallIcon(R.drawable.ic_notification)  // 小图标（必须）
               .setLargeIcon(BitmapFactory.decodeResource(
                   context.getResources(), 
                   R.drawable.ic_launcher
               ))  // 大图标（可选）
               .setContentIntent(pendingIntent)
               .setAutoCancel(true)  // 点击后自动取消
               .setDefaults(Notification.DEFAULT_ALL)  // 使用默认声音、振动、灯光
               .setWhen(System.currentTimeMillis())
               .setShowWhen(true);
        
        // 设置通知样式（可选，显示更多内容）
        Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
        bigTextStyle.bigText(content);
        builder.setStyle(bigTextStyle);
        
        // 显示通知
        int notificationId = (int) System.currentTimeMillis();
        notificationManager.notify(notificationId, builder.build());
    }
}

// ============================================
// 3. OppoPushMessageService.java - OPPO推送处理
// ============================================
package com.xunpaisoft.social;

import android.content.Context;
import com.heytap.msp.push.mode.DataMessage;
import com.heytap.msp.push.service.DataMessageCallbackService;

public class OppoPushMessageService extends DataMessageCallbackService {
    
    @Override
    public void processMessage(Context context, DataMessage dataMessage) {
        super.processMessage(context, dataMessage);
        
        // 获取推送消息内容
        String title = dataMessage.getTitle();
        String content = dataMessage.getContent();
        
        // 使用高优先级通知渠道显示通知
        PushNotificationHelper.showNotification(context, title, content);
    }
}

// ============================================
// 4. MainActivity.java - 权限申请（Android 13+）
// ============================================
package com.xunpaisoft.social;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
    
    private static final int REQUEST_NOTIFICATION_PERMISSION = 1001;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Android 13+ (API 33+) 需要动态申请通知权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) 
                != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.POST_NOTIFICATIONS},
                    REQUEST_NOTIFICATION_PERMISSION
                );
            }
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_NOTIFICATION_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限已授予，可以显示通知
            }
        }
    }
}

// ============================================
// 5. AndroidManifest.xml 配置
// ============================================
/*
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xunpaisoft.social">
    
    <!-- 通知权限 -->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
    <!-- 振动权限 -->
    <uses-permission android:name="android.permission.VIBRATE" />
    
    <!-- 唤醒锁（可选） -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- OPPO 推送服务 -->
        <service
            android:name=".OppoPushMessageService"
            android:permission="com.heytap.mcs.permission.SEND_PUSH_MESSAGE"
            android:exported="true">
            <intent-filter>
                <action android:name="com.heytap.mcs.action.RECEIVE_MCS_MESSAGE" />
            </intent-filter>
        </service>
        
    </application>
</manifest>
*/

