1. Are you ready for the Galaxy S20? Here is everything we know so far!

Why don't notifications show up when I run the app?

Discussion in 'Android Development' started by User123app, Mar 26, 2020.

  1. User123app

    User123app Lurker
    Thread Starter

    I'm a beginner programmer and I've been working on an app that shows events and notifies the user when a new event is up.I'm trying to create a settings activity with 2 switch buttons, 1 for notifications on status bar and 1 for notification sound. When I turn on the switches, I don't get any notification nor sound.

    How can I fix that?

    JAVA FILE

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.TaskStackBuilder;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.BitmapFactory;
    import android.os.Build;
    import android.support.v4.app.NotificationCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Switch;
    import android.widget.Toast;

    import androidx.annotation.RequiresApi;

    import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;


    public class Settings extends AppCompatActivity {
    Switch simpleSwitch1, simpleSwitch2;
    NotificationManager manager;
    Notification myNotication;
    private Notification notification;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);



    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
    simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
    if (simpleSwitch1.isChecked()){
    simpleSwitch1.setOnClickListener(new View.OnClickListener() {

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @override
    public void onClick(View arg0) {
    //API level 11
    Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

    PendingIntent pendingIntent = PendingIntent.getActivity(Settings.this, 1, intent, 0);

    Notification.Builder builder = new Notification.Builder(Settings.this);

    builder.setAutoCancel(false);
    builder.setTicker("this is ticker text");
    builder.setContentTitle("WhatsApp Notification");
    builder.setContentText("You have a new message");
    builder.setSmallIcon(R.drawable.notification);
    builder.setContentIntent(pendingIntent);
    builder.setOngoing(true);
    builder.setSubText("This is subtext..."); //API level 16
    builder.setNumber(100);
    builder.build();

    myNotication = builder.getNotification();
    manager.notify(11, myNotication);

    /*
    //API level 8
    Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis());

    Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
    PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0);
    myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2);
    manager.notify(11, myNotification8);
    */

    }
    });}
    else{
    simpleSwitch1.setOnClickListener(new View.OnClickListener() {

    @override
    public void onClick(View arg0) {
    manager.cancel(11);
    }
    });
    }


    }




    public void onClick(View v) {
    simpleSwitch2 = (Switch) findViewById(R.id.simpleswitch2);
    String statusSwitch2;

    if (simpleSwitch2.isChecked())
    notification.defaults |= Notification.DEFAULT_SOUND;




    }}


    XML FILE

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Settings">


    <TextView
    android:id="@+id/textView16"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:text="SETTINGS"
    android:textAppearance="@Style/Base.TextAppearance.AppCompat.Headline"
    android:textColor="@color/design_default_color_primary_dark"
    android:textColorHighlight="@color/design_default_color_primary"
    android:textStyle="bold" />

    <Switch
    android:id="@+id/simpleswitch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView16"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp"
    android:text="NOTIFICATIONS"
    android:textAppearance="@Style/Base.TextAppearance.AppCompat.Large"
    android:textColor="@color/colorPrimary"
    android:textStyle="normal"
    android:checked="true"
    />
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Why don't notifications
  1. Drchiefd113
    Replies:
    5
    Views:
    482
  2. kumaranil13k
    Replies:
    0
    Views:
    479
  3. kumaranil13k
    Replies:
    0
    Views:
    285
  4. Arron Mothersole
    Replies:
    3
    Views:
    917
  5. dorlow
    Replies:
    3
    Views:
    779
  6. JimmyPixel3a
    Replies:
    21
    Views:
    1,377
  7. iliji
    Replies:
    0
    Views:
    907
  8. dontpanicbobby
    Replies:
    46
    Views:
    1,426
  9. FensterBaron29
    Replies:
    5
    Views:
    7,534
  10. HeartOnFire69
    Replies:
    2
    Views:
    872

Share This Page

Loading...