Android - GCM push notifications not appearing in notifications list

前端 未结 2 1684
慢半拍i
慢半拍i 2021-01-12 17:26

I\'m working on my first Android app to use the Google Cloud Messaging (GCM) service for push notifications. I\'ve got to the point where I can successfully send a message

相关标签:
2条回答
  • 2021-01-12 17:52

    If you are using GcmListenerService you can use this code, add to your onMessageReceived the sendNotification()

    @Override
    public void onMessageReceived(String from, Bundle data) {
            String message = data.getString("message");
            sendNotification(message);
    }
    
    private void sendNotification(String message) {
            Intent intent = new Intent(this, YOURCLASS.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_park_notification)
                    .setContentTitle("Ppillo Message")
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
    
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    
    0 讨论(0)
  • 2021-01-12 18:09

    This code will generate a notification in the android system bar at the top of the screen. This code will create a new intent that will direct the user to a "Home.class" after clicking on the notification in the top bar. If you would like it to do something specific based on the current activity you could send broadcast requests from the GCMIntentService to your other activities.

    Intent notificationIntent=new Intent(context, Home.class);
    generateNotification(context, message, notificationIntent);
    
    private static void generateNotification(Context context, String message, Intent notificationIntent) {
        int icon = R.drawable.icon;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);
    
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }
    

    Note that this example uses resources in R.drawable and R.String that will need to be present to work but it should give you the idea. See this for more information about status notifications http://developer.android.com/guide/topics/ui/notifiers/index.html and this about broadcast recievers. http://developer.android.com/reference/android/content/BroadcastReceiver.html

    0 讨论(0)
提交回复
热议问题