Skip to content

Commit

Permalink
Merge branch 'feature/oreo' of https://github.com/slash-84/ti.goosh i…
Browse files Browse the repository at this point in the history
…nto slash-84-feature/oreo
  • Loading branch information
Jei committed Nov 6, 2018
2 parents 879f411 + d34142c commit b7c8b44
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ To get GCM sender ID:
* Find your API project and click Project Name.
* You can find Project Number. You will use it as the GCM sender ID.

## Set the channel name (basic support for Android Oreo)

In your tiapp.xml, insert

```xml
<property name="ti.goosh.defaultChannel">YOUR_CHANNEL_NAME</property>
```

<img src="oreo.png" />

### Crashes

If your app crashes on launch, try adding these in your tiapp.xml.
Expand Down
23 changes: 22 additions & 1 deletion android/src/ti/goosh/IntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.lang.Math;
import java.util.Random;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand All @@ -23,6 +25,7 @@
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
Expand All @@ -44,6 +47,9 @@ public class IntentService extends GcmListenerService {
private static final String LCAT = "ti.goosh.IntentService";
private static final AtomicInteger atomic = new AtomicInteger(0);

public static final String DEFAULT_CHANNEL_ID = "ti.goosh.defaultchannel";
public static final String DEFAULT_CHANNEL_NAME = "Push Notifications";

@Override
public void onMessageReceived(String from, Bundle bundle) {
Log.d(LCAT, "Push notification received from: " + from);
Expand Down Expand Up @@ -78,6 +84,15 @@ private Bitmap getBitmapFromURL(String src) throws Exception {
return BitmapFactory.decodeStream( new BufferedInputStream( connection.getInputStream() ) );
}

@TargetApi(26)
private NotificationChannel createOrUpdateDefaultNotificationChannel() {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String channelName = TiApplication.getInstance().getAppProperties().getString("ti.goosh.defaultChannel", DEFAULT_CHANNEL_NAME);
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
return channel;
}

private void parseNotification(Bundle bundle) {
TiGooshModule module = TiGooshModule.getModule();

Expand Down Expand Up @@ -161,8 +176,14 @@ private void parseNotification(Bundle bundle) {
PendingIntent contentIntent = PendingIntent.getActivity(this, new Random().nextInt(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);

// Start building notification
NotificationCompat.Builder builder = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(context, createOrUpdateDefaultNotificationChannel().getId());
} else {
builder = new NotificationCompat.Builder(context);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
int builder_defaults = 0;
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
Expand Down
Binary file added oreo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b7c8b44

Please sign in to comment.