繁体   English   中英

如何在Android + GCM中使用声音+自定义应用程序图标获取推送通知

[英]How to get a push notification with sound + custom app icon in Android + GCM

我正在为学校项目构建聊天应用程序。 到目前为止一切正常。 这样我就可以通过GCM获得推送通知。 但是这些只是默默显示。 您能帮我弄清楚如何制作声音(默认声音足够好)和我自己的应用程序图标吗?

编辑:无法使用Firebase。 我们必须在项目中使用GCM。

在下面,您可以看到我的代码来处理通知。 谢谢。

    NotificationHandler notificationHandler = new NotificationHandler(getApplicationContext());

    //If the app is in foreground
    if (!NotificationHandler.isAppIsInBackground(getApplicationContext())) {
        //Sending a broadcast to the chatroom to add the new message
        LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
    } else {
        //If app is not in foreground displaying push notification
        notificationHandler.showNotificationMessage(title, message);
    }

试试这个,对我有用

protected void displayNotification(Context context, String[] strContent) {
        try {
            int numMessages = 0;

            /* Invoking the default notification service */
            NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

            mBuilder.setContentTitle("New "+strContent[0].trim());
            mBuilder.setContentText("You've received new "+strContent[0].trim());
            mBuilder.setTicker("New "+strContent[0].trim()+" Alert!");
            mBuilder.setSmallIcon(R.drawable.ic_notif);
            mBuilder.setAutoCancel(true);
            mBuilder.setColor(context.getResources().getColor(R.color.ColorPrimary));
            mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
            mBuilder.setDefaults(Notification.DEFAULT_ALL);
            mBuilder.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000});         

            mBuilder.setLights(Color.RED, 3000, 3000);
         mBuilder.setNumber(++numMessages);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

            String[] events = new String[strContent.length-1];
            for(int i=0; i<strContent.length-1; i++){
                events[i] = new String(strContent[i+1].trim());
            }

            inboxStyle.setBigContentTitle("New "+strContent[0].trim()+" received!");

            for (int i=0; i < events.length; i++) {
                inboxStyle.addLine(events[i]);
            }

            mBuilder.setStyle(inboxStyle);

            /* Creates an explicit intent for an Activity in your app */
            Intent resultIntent = new Intent(this, HomeActivity.class);


            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(HomeActivity.class);

            /* Adds the Intent that starts the Activity to the top of the stack */
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            /* notificationID allows you to update the notification later on. */
            mNotificationManager.notify(9999, mBuilder.build());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM