android本地通知

该篇文章介绍了一个名为SoulWork的Android工作类,它继承自Worker,用于执行后台任务。任务包括接收FCM消息并创建通知。文章还展示了如何使用WorkManager来安排这些任务,并在特定延迟后执行。同时,当数据无效时,任务会返回成功状态。
摘要由CSDN通过智能技术生成
package org.cocos2dx.javascript.Soul;

import android.content.Context;
import android.util.Log;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import org.cocos2dx.javascript.SoulSdk;

public class SoulWork  extends Worker {

    private static final String TAG = "SoulWork";
    public SoulWork( Context appContext, WorkerParameters workerParams) {
        super(appContext, workerParams);
    }

    @Override
    public Result doWork() {
        Log.d(TAG, "Performing long running task in scheduled job");
        //接收外面传递进来的数据
        String body  = getInputData().getString("body");
        if(title == null || body == null || title.isEmpty() || body.isEmpty()){
            return Result.success();
        }
        sendNotification(body);

        Log.d(TAG, type);
        return Result.success();
    }

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_IMMUTABLE);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

1.延迟调用


    /**
     * 添加一个通知任务
     * @param conent 通知内容
     * @param seconds 通知下次时间
     */
    public static void addWork(Data conent, int seconds,String tag){
        WorkManager.getInstance( Cocos2dxHelper.getActivity()).cancelAllWorkByTag(tag);
        OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(SoulWork.class)
                .setInitialDelay(seconds, TimeUnit.SECONDS)
                .setInputData(conent)
                .addTag(tag)
                .build();
        WorkManager.getInstance( Cocos2dxHelper.getActivity()).beginWith(work).enqueue();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值