-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of android GCM push notifications
- Loading branch information
1 parent
8eac941
commit b37352a
Showing
30 changed files
with
446 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
android/app/src/main/java/com/zulipmobile/notifications/GCMPushNotifications.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.zulipmobile.notifications; | ||
|
||
import android.app.Notification; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
|
||
import com.wix.reactnativenotifications.core.AppLaunchHelper; | ||
import com.wix.reactnativenotifications.core.AppLifecycleFacade; | ||
import com.wix.reactnativenotifications.core.JsIOHelper; | ||
import com.wix.reactnativenotifications.core.notification.PushNotification; | ||
import com.zulipmobile.R; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class GCMPushNotifications extends PushNotification { | ||
|
||
public GCMPushNotifications(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) { | ||
super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper); | ||
} | ||
|
||
@Override | ||
protected PushNotificationsProp createProps(Bundle bundle) { | ||
return new PushNotificationsProp(bundle); | ||
} | ||
|
||
protected PushNotificationsProp getProps() { | ||
return (PushNotificationsProp) mNotificationProps; | ||
} | ||
|
||
@Override | ||
protected Notification.Builder getNotificationBuilder(PendingIntent intent) { | ||
// First, get a builder initialized with defaults from the core class. | ||
final Notification.Builder builder = super.getNotificationBuilder(intent); | ||
|
||
String type = getProps().getRecipientType(); | ||
String content = getProps().getContent(); | ||
String title = getProps().getSenderFullName(); | ||
String avatarURL = getProps().getAvatarURL(); | ||
String time = getProps().getTime(); | ||
String stream = getProps().getStream(); | ||
String topic = getProps().getTopic(); | ||
String baseURL = getProps().getBaseURL(); | ||
|
||
builder.setSmallIcon(R.drawable.zulip_notification); | ||
builder.setContentTitle(title); | ||
builder.setAutoCancel(true); | ||
builder.setContentText(content); | ||
|
||
if (type.equals("private")) { | ||
if (android.os.Build.VERSION.SDK_INT >= 16) { | ||
builder.setSubText("New private message"); | ||
} | ||
} else if (type.equals("stream")) { | ||
if (android.os.Build.VERSION.SDK_INT >= 16) { | ||
String displayTopic = stream + " > " | ||
+ topic; | ||
builder.setSubText("Mention on " + displayTopic); | ||
} | ||
} | ||
if (avatarURL != null) { | ||
Bitmap avatar = fetchAvatar(NotificationHelper.sizedURL(mContext, | ||
avatarURL, 64, baseURL)); | ||
if (avatar != null) { | ||
builder.setLargeIcon(avatar); | ||
} | ||
} | ||
if (time != null) { | ||
long timStamp = Long.parseLong(getProps().getTime()) * 1000; | ||
builder.setWhen(timStamp); | ||
} | ||
|
||
long[] vPattern = {0, 100, 200, 100}; | ||
builder.setVibrate(vPattern); | ||
return builder; | ||
} | ||
|
||
private Bitmap fetchAvatar(URL url) { | ||
try { | ||
return NotificationHelper.fetch(url); | ||
} catch (IOException e) { | ||
Log.e("ERROR", e.toString()); | ||
} | ||
return null; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
android/app/src/main/java/com/zulipmobile/notifications/NotificationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.zulipmobile.notifications; | ||
|
||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.util.Log; | ||
import android.util.TypedValue; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
|
||
public class NotificationHelper { | ||
|
||
public static Bitmap fetch(URL url) throws IOException { | ||
Log.i("GAFT.fetch", "Getting gravatar from url: " + url); | ||
URLConnection connection = url.openConnection(); | ||
connection.setUseCaches(true); | ||
Object response = connection.getContent(); | ||
if (response instanceof InputStream) { | ||
return BitmapFactory.decodeStream((InputStream) response); | ||
} | ||
return null; | ||
} | ||
|
||
public static URL sizedURL(Context context, String url, float dpSize, String baseUrl) { | ||
// From http://stackoverflow.com/questions/4605527/ | ||
Resources r = context.getResources(); | ||
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, | ||
dpSize, r.getDisplayMetrics()); | ||
try { | ||
return new URL(addHost(url, baseUrl) + "&s=" + px); | ||
} catch (MalformedURLException e) { | ||
Log.e("ERROR", e.toString()); | ||
return null; | ||
} | ||
} | ||
|
||
public static String addHost(String url, String baseURL) { | ||
if (!url.startsWith("http")) { | ||
if (baseURL.endsWith("/")) { | ||
url = baseURL.substring(0, baseURL.length() - 1) + url; | ||
} else { | ||
url = baseURL + url; | ||
} | ||
} | ||
return url; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
android/app/src/main/java/com/zulipmobile/notifications/PushNotificationsProp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.zulipmobile.notifications; | ||
|
||
import android.os.Bundle; | ||
|
||
import com.wix.reactnativenotifications.core.notification.PushNotificationProps; | ||
|
||
public class PushNotificationsProp extends PushNotificationProps { | ||
|
||
public PushNotificationsProp(Bundle bundle) { | ||
super(bundle); | ||
} | ||
|
||
public String getRecipientType() { | ||
return mBundle.getString("recipient_type"); | ||
} | ||
|
||
public String getContent() { | ||
return mBundle.getString("content"); | ||
} | ||
|
||
public String getSenderFullName() { | ||
return mBundle.getString("sender_full_name"); | ||
} | ||
|
||
public String getAvatarURL() { | ||
return mBundle.getString("sender_avatar_url"); | ||
|
||
} | ||
|
||
public String getStream() { | ||
return mBundle.getString("stream"); | ||
} | ||
|
||
public String getTopic() { | ||
return mBundle.getString("topic"); | ||
} | ||
|
||
public String getTime() { | ||
return mBundle.getString("time"); | ||
} | ||
|
||
@Override | ||
protected PushNotificationsProp copy() { | ||
return new PushNotificationsProp((Bundle) mBundle.clone()); | ||
} | ||
|
||
public String getEmail() { | ||
return mBundle.getString("sender_email"); | ||
} | ||
|
||
public String getBaseURL() { | ||
return mBundle.getString("base_url"); | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* @flow */ | ||
import { AppRegistry } from 'react-native'; | ||
import ZulipMobile from './src/ZulipMobile'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.