通过`PendingIntent`

时间:2019-11-19 02:02:01

标签: android-studio android-intent android-notifications android-pendingintent android-alarms

我正在尝试创建一个警报,该警报会在将来的特定时间弹出通知。我有下面的代码,大多数代码似乎都在工作,但是由于某种原因,我似乎无法在Intent对象中传递任何值。我觉得我做对了所有事情。我什至看过几本在线教程,据我所知,我正在做与他们完全相同的事情,但是他们的工作和我的工作不一样。

Receiver.java

package com.example.mytermtracker.notifications;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.mytermtracker.application.Date;

public class Receiver extends BroadcastReceiver {
    private static final String TAG = "app: Receiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive: now:     " + Date.now().toSQL());

        Channel channel = intent.getParcelableExtra("channel");
        if (channel == null) 
            throw new NullPointerException("Could not find the channel."); //<== the line that the breaks.

        String title = intent.getStringExtra("title");
        if (title == null) title = channel.NAME;

        String message = intent.getStringExtra("message");
        if (message == null) message = "";

        int callerID = intent.getIntExtra("caller_id", 0);

        channel.show(context, title, message, callerID);
    }

    public static void setAlarm(Context context, Channel channel, String title, String message, Date trigger, int callerID) {
        Intent intent = new Intent(context, Receiver.class);
        intent.putExtra("channel", channel);
        intent.putExtra("title", title);
        intent.putExtra("message", message);
        intent.putExtra("caller_id", callerID);

        Log.d(TAG, "setAlarm : now:     " + Date.now().toSQL());
        Log.d(TAG, "setAlarm : trigger: " + trigger.toSQL());

        PendingIntent sender = PendingIntent
                .getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, trigger.getTimeInMillis(), sender);
        }
    }
}

Channel.java

package com.example.mytermtracker.notifications;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.mytermtracker.R;
import com.example.mytermtracker.application.Date;

import static com.example.mytermtracker.application.App.MANAGER;

public class Channel implements Parcelable {
    private static final String TAG = "app: Channel";

    public final String ID;
    public final String NAME;
    public final String DESCRIPTION;
    public final int IMPORTANCE;

    private Channel(NotificationManager manager, String id, String name, String description, int importance) {
        this.ID = id;
        this.NAME = name;
        this.DESCRIPTION = description;
        this.IMPORTANCE = importance;

        if (manager != null) {
            NotificationChannel termStartChannel = new NotificationChannel(ID, NAME, IMPORTANCE);
            termStartChannel.setDescription(DESCRIPTION);
            manager.createNotificationChannel(termStartChannel);
        }
    }

    protected Channel(Parcel in) {
        ID = in.readString();
        NAME = in.readString();
        DESCRIPTION = in.readString();
        IMPORTANCE = in.readInt();
    }

    public static final Creator<Channel> CREATOR = new Creator<Channel>() {
        @Override
        public Channel createFromParcel(Parcel in) {
            return new Channel(in);
        }

        @Override
        public Channel[] newArray(int size) {
            return new Channel[size];
        }
    };

    public static Channel create(NotificationManager manager, String id, String name, String description) {
        return new Channel(manager, id, name, description, NotificationManager.IMPORTANCE_DEFAULT);
    }
    public static Channel create(NotificationManager manager, String id, String name, String description, int importance) {
        return new Channel(manager, id, name, description, importance);
    }

    public void show(Context context, String title, String message, int callerID) {
        Log.d(TAG, "show: now: " + Date.now().toSQL());
        Notification notification = new NotificationCompat.Builder(context, ID)
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setContentTitle(title)
                .setContentText(message)
                .build();

        MANAGER.notify(callerID, notification);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(ID);
        dest.writeString(NAME);
        dest.writeString(DESCRIPTION);
        dest.writeInt(IMPORTANCE);
    }
}

1 个答案:

答案 0 :(得分:0)

好的。所以我想通了。显然FLAG_UPDATE_CURRENT对输入造成了混乱。我只是将其更改为FLAG_IMMUTABLE,这似乎已经解决了该问题。