通知不再起作用。如何解决?

时间:2019-06-03 17:13:11

标签: java android

我的应用程序实现了5种类型的自定义通知。 2月,一切正常。 我现在正在处理它,并且不再显示通知。

首先,我使用Firebase云消息传递进行了测试。通知已成功显示在我的设备上。 然后,我尝试按照以下答案修改代码: Notification not showing in Oreo。 没有成功。

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.philipp.us">

    <uses-permission
        android:name="android.permission.GET_ACCOUNTS"
        android:maxSdkVersion="27" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <!-- for android -->
    <!-- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/> -->
    <!-- <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/> -->
    <!-- <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> -->
    <!-- <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> -->


    <!-- for Samsung -->
    <uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
    <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

    <!-- for htc -->
    <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" />

    <!-- for sony -->
    <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
    <uses-permission android:name="com.sonymobile.home.permission.PROVIDER_INSERT_BADGE" />

    <!-- for apex -->
    <uses-permission android:name="com.anddoes.launcher.permission.UPDATE_COUNT" />

    <!-- for solid -->
    <uses-permission android:name="com.majeur.launcher.permission.UPDATE_BADGE" />

    <!-- for huawei -->
    <uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
    <uses-permission android:name="com.huawei.android.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.huawei.android.launcher.permission.WRITE_SETTINGS" />

    <!-- for ZUK -->
    <uses-permission android:name="android.permission.READ_APP_BADGE" />

    <!-- for OPPO -->
    <uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS" />

    <!-- for EvMe -->
    <uses-permission android:name="me.everything.badger.permission.BADGE_COUNT_READ" />
    <uses-permission android:name="me.everything.badger.permission.BADGE_COUNT_WRITE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission
        android:name="android.permission.INTERACT_ACROSS_USERS_FULL"
        android:protectionLevel="signature"
        tools:ignore="ProtectedPermissions" />

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

    <application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.philipp.android.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Translucent"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
 ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.example.philipp.android.ConversationActivity"
            android:label="@string/menu_item_conversations"
            android:windowSoftInputMode="stateHidden|adjustResize" />

        <service android:name="com.example.philipp.android.PushMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name="com.example.philipp.android.PushIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <!--
      Set custom default icon. This is used when no icon is set for incoming notification messages.

        -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@mipmap/ic_launcher" />
        <!--
         Set color used with incoming notification messages. This is used when no color is set for the incoming

        -->
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />

        <activity
            android:name="com.example.philipp.android.UserProfileActivity"
            android:label="@string/menu_item_userprofile"
            android:windowSoftInputMode="stateHidden|adjustPan" />

        <provider
            android:name="com.example.philipp.android.GenericFileProvider"
            android:authorities="${applicationId}.my.package.name.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

        <receiver
            android:name="com.example.philipp.android.PushMessagingService$NotificationReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="NOTIFICATION_RECEIVE" />
            </intent-filter>
        </receiver>

        <activity
            android:name="com.example.philipp.android.SubscriptionActivity"
            android:label="@string/title_activity_subscription"
            android:theme="@style/AppTheme.NoActionBar"/>
    </application>

</manifest>

这是我的第一个自定义通知:


    DataBaseHelper2 dbHelper2;

    File storageDir;
    Context context;
    FirebaseStorage storage = FirebaseStorage.getInstance();

    public PushMessagingService() {
        context = this;
    }

    public static class NotificationID {
        private static final  AtomicInteger c = new AtomicInteger(5);
        public static int getID() {
            return c.incrementAndGet();
        }
    }
    public static int notifbadgeCount = 0;

    final String TAG = "PushMessageReceiver";

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        notifbadgeCount++;
        ShortcutBadger.applyCount(context, notifbadgeCount); //for 1.1.4+

        dbHelper2 = new DataBaseHelper2();
        storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        Log.d(TAG, remoteMessage.toString());

        final Map<String, String> data = remoteMessage.getData();
        Log.d(TAG, data.get("message") + " " + data.get("sender") + " " + data.get("sender_id") + " " + data.get("type") + " " + data.get("id") + " " + data.get("photo"));
        int icon = R.drawable._logo_us_without_bkgrdxxxhdpi;
        long when = System.currentTimeMillis();

        if (isRunning(this) && data.get("type").equals("greeting")) {
            Log.d(TAG, "GREETING");
        }


//KEYWORD MATCH
        if (data.get("type").equals("question")) {

            Log.d(TAG, "QUESTION");

// Get the layouts to use in the custom notification
//NORMALSIZE
            //Set Notification remote Layout
            RemoteViews notificationLayoutkeywordmatch = new RemoteViews(getPackageName(), R.layout.notification_keywordmatch);
            //User Photo
            notificationLayoutkeywordmatch.setImageViewResource(R.id.img_userprofilpic, R.drawable._btn_addprofilpicmdpi);
            //Header
            notificationLayoutkeywordmatch.setImageViewResource(R.id.img_applogo, R.drawable._logo_us_without_bkgrdmdpi);
            notificationLayoutkeywordmatch.setTextViewText(R.id.notification_app_name, "ask Us");
            notificationLayoutkeywordmatch.setTextViewText(R.id.lbl_date_and_time, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
            notificationLayoutkeywordmatch.setImageViewResource(R.id.img_notification_type, R.drawable._ic_questionxxxhdpi);
            notificationLayoutkeywordmatch.setImageViewResource(R.id.notification_arrow_down, R.drawable.simple_arrow_downmdpi);
            //Body
            notificationLayoutkeywordmatch.setTextViewText(R.id.notification_keywordmatch_text, getString(R.string.notif_keyword_match));
            notificationLayoutkeywordmatch.setTextViewText(R.id.notification_keywordmatch_sender, data.get("sender"));
            notificationLayoutkeywordmatch.setTextViewText(R.id.notification_keywordmatch_question, data.get("message"));

//EXSTENDED SIZE
            //Set Notification remote Layout
            RemoteViews notificationLayoutkeywordmatchexpanded = new RemoteViews(getPackageName(), R.layout.notification_keywordmatchexpanded);
            //User Photo
            notificationLayoutkeywordmatchexpanded.setImageViewResource(R.id.img_userprofilpic, R.drawable._btn_addprofilpicmdpi);
            //Header
            notificationLayoutkeywordmatchexpanded.setImageViewResource(R.id.img_applogo, R.drawable._logo_us_without_bkgrdmdpi);
            notificationLayoutkeywordmatchexpanded.setTextViewText(R.id.notification_app_name, "ask Us");
            notificationLayoutkeywordmatchexpanded.setTextViewText(R.id.lbl_date_and_time, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
            notificationLayoutkeywordmatchexpanded.setImageViewResource(R.id.notification_arrow_up, R.drawable.simple_arrow_upmdpi);
            //Body
            notificationLayoutkeywordmatchexpanded.setTextViewText(R.id.notification_keywordmatch_text, getString(R.string.notif_keyword_match));
            notificationLayoutkeywordmatchexpanded.setImageViewResource(R.id.img_notification_type, R.drawable._ic_questionxxxhdpi);
            notificationLayoutkeywordmatchexpanded.setTextViewText(R.id.notification_keywordmatch_sender, data.get("sender")+ ":");
            notificationLayoutkeywordmatchexpanded.setTextViewText(R.id.notification_keywordmatch_question, data.get("message"));

//Set Notification Channel
            final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            int notificationId = 1;
            String channelId = "channel-01";
            String channelName = "Someone could need you";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        channelId, channelName, importance);
                mNotificationManager.createNotificationChannel(mChannel);
            }


//Set Pending Intent
            final Intent ii = new Intent(context.getApplicationContext(), ConversationActivity.class);
            ii.putExtra("questionId", data.get("id"));
            ii.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, ii, PendingIntent.FLAG_UPDATE_CURRENT);

// Apply the layouts to the notification
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable._logo_us_without_bkgrdmdpi)
                    .setCustomContentView(notificationLayoutkeywordmatch)
                    .setCustomBigContentView(notificationLayoutkeywordmatchexpanded)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setChannelId(channelId);

            // Build notification

            final Notification notification = mBuilder.build();

            final NotificationTarget notificationTarget = new NotificationTarget(context, R.id.img_userprofilpic, notificationLayoutkeywordmatch, notification, 0);
            final NotificationTarget notificationTargetExpanded = new NotificationTarget(context, R.id.img_userprofilpic, notificationLayoutkeywordmatchexpanded, notification, 0);


            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    Glide.with(getApplicationContext())
                            .asBitmap()
                            .load(data.get("photo"))
                            .into(notificationTarget);

                    Glide.with(getApplicationContext())
                            .asBitmap()
                            .load(data.get("photo"))
                            .into(notificationTargetExpanded);
                }
            });


            mNotificationManager.notify(notificationId, notification);

        }```


Thank you for help.

1 个答案:

答案 0 :(得分:0)

oreo上方的android不会在前台显示通知。您需要在警报对话框中显示它