来自其他班级的来电通知

时间:2013-12-08 14:01:20

标签: java android android-notifications

鉴于在我的应用程序中,我需要很多通知,我有类似的类。

public class MyNotificationClass{

    NotificationManager mNotificationManager;
    NotificationCompat.Builder notificationBuilder;
    Context context;

    static final int NOTIFICA1 = 1;
    static final int NOTIFICA2 = 2;

    public MyNotificationClass(Context context) {
        // TODO Auto-generated constructor stub
        mNotificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuilder = new NotificationCompat.Builder(context);
    }

    public void mostra(int Id) {

        switch(Id)
        {
        case NOTIFICA1:
            notificationBuilder.setContentTitle("Text01");
            notificationBuilder.setContentText("Text01");
            mNotificationManager.notify(NOTIFICA1, notificationBuilder.build());
            break;
        case NOTIFICA2:
            notificationBuilder.setContentTitle("Text02");
            notificationBuilder.setContentText("Text02");
            mNotificationManager.notify(NOTIFICA2, notificationBuilder.build());
            break;
        }
        notificationBuilder.setWhen(System.currentTimeMillis());
        notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS );
    }

}

如果我想打电话,例如,通知编号1.我该怎么办?

2 个答案:

答案 0 :(得分:1)

setWhen(), setSmallIcon(), setDefaults()通知之前,您必须在NotificationBuilder上调用build()。将该代码移到switch-block前面。

重要电话是 setSmallIcon()。如果您没有设置图标,则不会显示通知。

试试这样:

public void mostra(int Id) {

    notificationBuilder.setWhen(System.currentTimeMillis());
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS );

    switch(Id)
    {
    case NOTIFICA1:
        notificationBuilder.setContentTitle("Text01");
        notificationBuilder.setContentText("Text01");
        mNotificationManager.notify(id, notificationBuilder.build());
        break;
    case NOTIFICA2:
        notificationBuilder.setContentTitle("Text02");
        notificationBuilder.setContentText("Text02");
        mNotificationManager.notify(id, notificationBuilder.build());
        break;
    }
}

编辑:

在您的活动中调用此方法:

MyNotificationClass myNoti = new MyNotificationClass(this);
myNoti.mostra(MyNotificationClass.NOTIFICA1);

答案 1 :(得分:0)

public void onMessageReceived(RemoteMessage remoteMessage) {
        String title = remoteMessage.getFrom();
        String body = remoteMessage.getNotification().getBody();
        String imageUri;
        Bitmap picture;

        if (remoteMessage.getData().get("image") != null) {

            imageUri = remoteMessage.getData().get("image");
            picture = getBitmapfromUrl(imageUri);

        } else {

            picture = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.bg);

        }

        // This below code is for display image in notificaation
        NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
        notiStyle.setSummaryText(body);
        notiStyle.bigPicture(picture);


        Intent resultIntent = new Intent(this, LoginActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                0 /* Request code */, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setSmallIcon(R.drawable.logo);
        mBuilder.setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(false)
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setContentIntent(resultPendingIntent)
                .setStyle(notiStyle);


        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        assert mNotificationManager != null;

        mNotificationManager.notify(new Random().nextInt() /* Request Code */, mBuilder.build());


    }


---------------------------------------------------------------------------

Call this method like this in your Activity:

MyFirebaseMessagingService myfirebasemsg = new MyFirebaseMessagingService();
myfirebasemsg.onMessageReceived(remoteMessage);