成功为移动应用付款后推送通知

时间:2019-03-23 17:51:01

标签: notifications push

我需要帮助,该如何通过付款成功向用户发送推送通知

1 个答案:

答案 0 :(得分:0)

如果要在后台检查交易状态,请使用one of the background workers。我将使用WorkManager

implementation "android.arch.work:work-runtime:1.0.1"

public class SynchronizeWorker extends Worker {

    static final String TAG = "MYWORKER";

    public SynchronizeWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    private Notification getNotification() {
        NotificationManager mNotificationManager =
                (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("default",
                    "WTF_CHANNEL",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("WTF_CHANNEL_DESCRIPTION");
            mNotificationManager.createNotificationChannel(channel);
        }

        // specify the class of the activity you want to open after click on notification

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                getApplicationContext(),
                0,
                intent,
                0);

        return new NotificationCompat.Builder(getApplicationContext(), "default")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getApplicationContext().getResources().getString(R.string.notification_header))
                .setContentText(getApplicationContext().getResources().getString(R.string.notification_text))
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .build();
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d(TAG, "status checking started");

        // check your transaction status here and show notification
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            Log.d(TAG, "status checking finished");
        }
        Log.d(TAG, "status checking finished");

        NotificationManagerCompat
                .from(getApplicationContext())
                .notify((int)(Math.random() * 10), getNotification());

        return Result.success();
    }
}

然后将工作人员排队到要检查状态的地方

OneTimeWorkRequest myWorkRequest = new OneTimeWorkRequest.Builder(SynchronizeWorker.class).build();
WorkManager.getInstance().enqueue(myWorkRequest);

我了解这可能并非您真正想要的,但希望它能以某种方式提供帮助:)