Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #86 from aerogear/fix-helloworld-push-notification
Browse files Browse the repository at this point in the history
Fix helloworld push notification
  • Loading branch information
secondsun committed Sep 24, 2019
2 parents 8f6de33 + 9b6db67 commit 168e6df
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 94 deletions.
3 changes: 2 additions & 1 deletion HelloPush/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
android:name=".HelloWorldApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
android:label="@string/app_name"
android:usesCleartextTraffic="true">

<meta-data
android:name="DEFAULT_MESSAGE_HANDLER_KEY"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@

import android.app.Application;

import org.jboss.aerogear.android.core.Callback;
import org.jboss.aerogear.android.unifiedpush.RegistrarManager;
import org.jboss.aerogear.android.unifiedpush.fcm.AeroGearFCMPushRegistrar;
import org.jboss.aerogear.android.unifiedpush.metrics.UnifiedPushMetricsMessage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -48,10 +43,4 @@ public void addMessage(String newMessage) {
messages.add(newMessage);
}

public void sendMetric(UnifiedPushMetricsMessage metricsMessage, Callback<UnifiedPushMetricsMessage> callback) {
AeroGearFCMPushRegistrar registrar = (AeroGearFCMPushRegistrar)
RegistrarManager.getRegistrar(PUSH_REGISTER_NAME);
registrar.sendMetrics(metricsMessage, callback);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import org.jboss.aerogear.android.unifiedpush.MessageHandler;
import org.jboss.aerogear.android.unifiedpush.RegistrarManager;
import org.jboss.aerogear.android.unifiedpush.fcm.UnifiedPushMessage;
import org.jboss.aerogear.android.unifiedpush.metrics.UnifiedPushMetricsMessage;
import org.jboss.aerogear.android.cookbook.hellopush.HelloWorldApplication;
import org.jboss.aerogear.android.cookbook.hellopush.R;
import org.jboss.aerogear.android.cookbook.hellopush.callback.MetricsCallback;
import org.jboss.aerogear.android.cookbook.hellopush.handler.NotificationBarMessageHandler;

public class MessagesActivity extends AppCompatActivity implements MessageHandler {
Expand All @@ -44,13 +42,8 @@ protected void onCreate(Bundle savedInstanceState) {

application = (HelloWorldApplication) getApplication();

if(getIntent().getBooleanExtra(HelloWorldApplication.PUSH_MESSAGE_FROM_BACKGROUND, false)) {
UnifiedPushMetricsMessage metricsMessage = new UnifiedPushMetricsMessage(getIntent().getExtras());
application.sendMetric(metricsMessage, new MetricsCallback());
}

View emptyView = findViewById(R.id.empty);
listView = (ListView) findViewById(R.id.messages);
listView = findViewById(R.id.messages);
listView.setEmptyView(emptyView);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,94 @@
*/
package org.jboss.aerogear.android.cookbook.hellopush.handler;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationManagerCompat;

import org.jboss.aerogear.android.unifiedpush.MessageHandler;
import org.jboss.aerogear.android.unifiedpush.fcm.UnifiedPushMessage;
import org.jboss.aerogear.android.cookbook.hellopush.HelloWorldApplication;
import org.jboss.aerogear.android.cookbook.hellopush.R;
import org.jboss.aerogear.android.cookbook.hellopush.activities.MessagesActivity;
import org.jboss.aerogear.android.unifiedpush.MessageHandler;
import org.jboss.aerogear.android.unifiedpush.fcm.UnifiedPushMessage;

public class NotificationBarMessageHandler implements MessageHandler {

public static final int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "aerogear-channel";
private static final int NOTIFICATION_ID = 1;

public static final NotificationBarMessageHandler instance = new NotificationBarMessageHandler();

// This should be public to be used on AndroidManifest.xml
@SuppressWarnings("WeakerAccess")
public NotificationBarMessageHandler() {
}

@Override
public void onMessage(Context context, Bundle bundle) {

String message = bundle.getString(UnifiedPushMessage.ALERT_KEY);

HelloWorldApplication application = (HelloWorldApplication) context.getApplicationContext();
application.addMessage(message);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel(context);
}

notify(context, bundle);

}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel(Context context) {

String name = "AeroGear";
String description = "AeroGear Android Cookbook";
int importance = NotificationManager.IMPORTANCE_DEFAULT;

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);

NotificationManager systemService = context.getSystemService(NotificationManager.class);
systemService.createNotificationChannel(channel);

}

private void notify(Context context, Bundle bundle) {
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);

String message = bundle.getString(UnifiedPushMessage.ALERT_KEY);
String pushMessageId = bundle.getString(UnifiedPushMessage.PUSH_MESSAGE_ID);

Intent intent = new Intent(context, MessagesActivity.class)
.addFlags(PendingIntent.FLAG_UPDATE_CURRENT)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
.putExtra(UnifiedPushMessage.ALERT_KEY, message)
.putExtra(UnifiedPushMessage.PUSH_MESSAGE_ID, pushMessageId)
.putExtra(HelloWorldApplication.PUSH_MESSAGE_FROM_BACKGROUND, true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, 0, intent, 0);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
Builder builder = new Builder(context, CHANNEL_ID)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(message);
.setContentIntent(pendingIntent);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, builder.build());

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
9 changes: 9 additions & 0 deletions HelloPush/app/src/main/res/drawable/ic_notifications_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,18.69L7.84,6.14 5.27,3.49 4,4.76l2.8,2.8v0.01c-0.52,0.99 -0.8,2.16 -0.8,3.42v5l-2,2v1h13.73l2,2L21,19.72l-1,-1.03zM12,22c1.11,0 2,-0.89 2,-2h-4c0,1.11 0.89,2 2,2zM18,14.68L18,11c0,-3.08 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68c-0.15,0.03 -0.29,0.08 -0.42,0.12 -0.1,0.03 -0.2,0.07 -0.3,0.11h-0.01c-0.01,0 -0.01,0 -0.02,0.01 -0.23,0.09 -0.46,0.2 -0.68,0.31 0,0 -0.01,0 -0.01,0.01L18,14.68z"/>
</vector>
2 changes: 1 addition & 1 deletion HelloPush/app/src/main/res/layout/messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<ImageView android:layout_width="36dp"
android:layout_height="40dp"
android:src="@drawable/ic_notifications"
android:src="@drawable/ic_notifications_off"
android:layout_marginBottom="20dp"
android:contentDescription="@string/notification_icon"/>

Expand Down
21 changes: 0 additions & 21 deletions HelloPush/app/src/main/res/values-v21/styles.xml

This file was deleted.

2 changes: 1 addition & 1 deletion HelloPush/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.google.gms:google-services:4.2.0'
}
}
Expand Down

0 comments on commit 168e6df

Please sign in to comment.