【问题标题】:push Notification in android with android studio使用 android studio 在 android 中推送通知
【发布时间】:2017-09-04 20:25:51
【问题描述】:

我会在 android studio 上调用 Notification 当我从应用程序中发送消息时,仅显示图标和文本,但不播放声音或振动 当我在应用程序内时,播放声音和振动。 有什么帮助吗? 我有清单 演出通知方式:

Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

            //Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Uri  defaultSoundUri = Uri.parse("android.resource://"+this.getPackageName()+"/"+R.raw.power);
            //Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            Notification.Builder notificationBuilder = new Notification.Builder(this)
                    .setSmallIcon(R.mipmap.colantaico)
                    .setContentTitle(Titulo)
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
                    .setLights(Color.RED, 3000, 3000)
                    .setContentIntent(pendingIntent);

            Notification mNotification = notificationBuilder.build();

            mNotification.flags |= Notification.FLAG_INSISTENT;
            mNotification.sound = defaultSoundUri;
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0 /* ID of notification */, mNotification);

在清单中

 <uses-permission android:name="android.permission.VIBRATE" />
<service
            android:name=".RecibidorNotificacionesFCM">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

【问题讨论】:

    标签: android firebase notifications firebase-cloud-messaging


    【解决方案1】:
    private static final int NOTIFICATION_ID = 1;
    
    Intent  openIntent = new Intent(this, MainActivity.class);;
    PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
                    openIntent, PendingIntent.FLAG_ONE_SHOT);
    
    NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setVibrate(new long[]{100, 250, 100, 250, 100, 250})
                            .setAutoCancel(true)
                            .setColor(this.getResources().getColor(R.color.activity_toolbar_color))
                            .setContentTitle("Test Notification")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(Html.fromHtml("Notification text")))
                            .setPriority(Notification.PRIORITY_MAX)
                            .setContentText(Html.fromHtml("Notification text")))
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mBuilder.setSmallIcon(R.drawable.notification_icon1);
    } else {
                mBuilder.setSmallIcon(R.drawable.notification_icon);
    }
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDeleteIntent(LocalNotificationEventReceiver.getDeleteIntent(this));
    
    
    NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    Notification notification = mBuilder.build();
    notificationManager.notify(NOTIFICATION_ID, notification);
    

    【讨论】:

    • 对不起,不,保持不变,没有声音或振动
    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多