Android notification with buttons on it

前端 未结 6 629
暖寄归人
暖寄归人 2020-11-30 02:06

I\'m trying to make a notification with 2 buttons on it:

  • one takes me back to the activity
  • the other closes it

Has anyone got an idea o

6条回答
  •  独厮守ぢ
    2020-11-30 02:38

    For me, this is working greatly.. I will write down the whole example.. This is for Notification create

    public void createNotification2(String aMessage) {
        final int NOTIFY_ID = 11;
        String name = getString(R.string.app_name);
        String id = getString(R.string.app_name); // The user-visible name of the channel.
        String description = getString(R.string.app_name); // The user-visible description of the channel.
        NotificationCompat.Builder builder;
        if (notifManager == null) {
            notifManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notifManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, name, importance);
                mChannel.setDescription(description);
                mChannel.enableVibration(true);
                mChannel.setLightColor(getColor(R.color.colorPrimaryDark));
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notifManager.createNotificationChannel(mChannel);
            }
        } else {
    
        }
        Intent Off_broadcastIntent = new Intent(this, Database_Update.class);
        Off_broadcastIntent.setAction("on");
        Off_broadcastIntent.putExtra("toastMessage", "1");
        PendingIntent Off_actionIntent = PendingIntent.getService(this, 0, Off_broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        Intent on_broadcastIntent = new Intent(this, Database_Update.class);
        on_broadcastIntent.setAction("off");
        on_broadcastIntent.putExtra("toastMessage", "0");
        PendingIntent on_actionIntent = PendingIntent.getService(this, 0, on_broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        Intent cancel_broadcastIntent = new Intent(this, Database_Update.class);
        cancel_broadcastIntent.setAction("cancel");
        cancel_broadcastIntent.putExtra("toastMessage", "close");
        PendingIntent cancel_actionIntent = PendingIntent.getService(this, 0, cancel_broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        Intent content_intent = new Intent(this, Status_Page.class);
        content_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, content_intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, id)
                .setSmallIcon(android.R.drawable.ic_popup_reminder)
                .setContentTitle(name)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setAutoCancel(false)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .addAction(R.drawable.block, "ON", Off_actionIntent)
                .addAction(R.drawable.notification, "OFF", on_actionIntent)
                .addAction(R.drawable.clear, "CLOSE", cancel_actionIntent);
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_NO_CLEAR|Notification.FLAG_ONGOING_EVENT;
        notifManager.notify(11, notification);
    }
    

    In Android Menifest

    
    

    This is service class

    public class Database_Update extends Service {
    String result="";
    
    Realm realm;
    BlockList blockList;
    @Override
    public void onCreate() {
        try {
            RealmConfiguration config = new RealmConfiguration.Builder()
                    .name("notification.realm")
                    .schemaVersion(1)
                    .deleteRealmIfMigrationNeeded()
                    .build();
            realm = Realm.getInstance(config);
    
        } catch (Exception e) {
    
            Log.d("Error Line Number", Log.getStackTraceString(e));
        }
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        Log.d("SERVICE","SERVICE CHECKING");
        result=intent.getStringExtra("toastMessage");
        Log.d("SERVICE",result);
        if (realm!=null){
            Log.d("SERVICE","realm working");
        }else {
            Log.d("SERVICE","Realm not working");
        }
        blockList=realm.where(BlockList.class).equalTo("package_name", "BLOCK_ALL").findFirst();
        try {
            Log.d("SERVICE",blockList.getStatus());
        } catch (Exception e) {
            Log.d("Error Line Number", Log.getStackTraceString(e));
        }
        realm.beginTransaction();
        if (result.equals("1")){
            if (blockList==null){
                BlockList blockList_new=realm.createObject(BlockList.class);
                blockList_new.setPackage_name("BLOCK_ALL");
                blockList_new.setStatus("yes");
            }else {
                blockList.setStatus("yes");
            }
            Log.d("SERVICE","BLOCKING NOTIFICATION");
            Toast.makeText(this, "BLOCKING", Toast.LENGTH_SHORT).show();
        }else if (result.equals("0")){
            if (blockList==null){
                BlockList blockList_new=realm.createObject(BlockList.class);
                blockList_new.setPackage_name("BLOCK_ALL");
                blockList_new.setStatus("no");
            }else {
                blockList.setStatus("no");
            }
            Log.d("SERVICE","ALLOW NOTIFICATION");
            Toast.makeText(this, "ALLOW NOTIFICATION", Toast.LENGTH_SHORT).show();
        }else if (result.equals("close")){
            NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(11);
            Log.d("SERVICE","REMOVING");
            Toast.makeText(this, "CLOSED", Toast.LENGTH_SHORT).show();
        }
        realm.commitTransaction();
        return START_STICKY;
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }
    
    @Override
    public void onDestroy() {
        if (realm!=null){
            realm.close();
        }
        Toast.makeText(this, "REMOVING", Toast.LENGTH_SHORT).show();
    }
    }
    

提交回复
热议问题