未显示通知

问题描述 投票:0回答:1

我不熟悉编程和应用开发,我试图开发一个显示事件并在新事件发生时通知用户的应用。我正在尝试通过2个开关按钮创建一个设置活动,其中1个用于状态栏上的通知和1的通知声音。当我打开开关时,没有任何通知或声音。

我该如何解决?

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文件

<?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"
        />

    <Switch
        android:id="@+id/simpleswitch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/simpleswitch1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="SOUNDS"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="@color/colorPrimary"
        android:checked="true" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="61dp"
        android:layout_height="44dp"
        android:layout_marginLeft="280dp"
        android:layout_marginTop="30dp"
        app:srcCompat="@drawable/ic_menu_manage" />

</RelativeLayout>
java notifications switch-statement
1个回答
0
投票

这里有几件事:

© www.soinside.com 2019 - 2024. All rights reserved.