用android studio在android中推送通知

时间:2017-09-04 20:25:52

标签: android firebase notifications firebase-cloud-messaging

我做下一个用于在android studio上调用通知 当我从应用程序发送消息时,只显示图标和文本,但不播放声音或振动 当我在应用程序内部时,播放声音和振动。 有帮助吗? 我有清单 显示通知的方法:

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>

1 个答案:

答案 0 :(得分:0)

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);