Created
September 9, 2016 09:27
-
-
Save benznest/331db4b521d700c5b77664b894a8f971 to your computer and use it in GitHub Desktop.
Benznest studios test about using Android notifications.
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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:padding="16dp" | |
tools:context="com.benzneststudios.testnotification.MainActivity"> | |
<Button | |
android:id="@+id/btn_show_notification" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:text="Show notification" /> | |
</FrameLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.benzneststudios.testnotification.Main2Activity"> | |
<TextView | |
android:id="@+id/tv_answer" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:textSize="36sp" | |
android:text="Test" | |
android:layout_gravity="center_horizontal"/> | |
</FrameLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.benzneststudios.testnotification"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".Main2Activity"></activity> | |
</application> | |
</manifest> |
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
package com.benzneststudios.testnotification; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
public class Main2Activity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main2); | |
TextView tv = (TextView) findViewById(R.id.tv_answer); | |
Bundle bundle = getIntent().getExtras(); | |
if(bundle != null){ | |
tv.setText(bundle.getString("MESSAGE")); | |
} | |
} | |
} |
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
package com.benzneststudios.testnotification; | |
import android.annotation.TargetApi; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.support.v4.app.NotificationCompat; | |
import android.support.v4.app.RemoteInput; | |
import android.support.v4.app.TaskStackBuilder; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
public class MainActivity extends AppCompatActivity { | |
Button btnShowNotification; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
btnShowNotification = (Button) findViewById(R.id.btn_show_notification); | |
btnShowNotification.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
showNotification(); | |
} | |
}); | |
} | |
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH) | |
private void showNotification() { | |
Context context = MainActivity.this; | |
int color = ContextCompat.getColor(context, R.color.colorPrimary); | |
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), | |
R.mipmap.ic_launcher); | |
final NotificationManager notificationManager = | |
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |
final String KEY_TEXT_REPLY = "ACTION_REPLY"; | |
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY) | |
.setLabel("REPLY") | |
.build(); | |
NotificationCompat.Action action = | |
new NotificationCompat.Action.Builder(R.mipmap.ic_launcher,"OK", getPendingIntent(context,"Hello World")) | |
.build(); | |
final NotificationCompat.Builder notification = new NotificationCompat.Builder(context) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setLargeIcon(bitmap) | |
.setContentTitle("Hello World") | |
.setContentText("Good night.") | |
.setAutoCancel(true) | |
.setColor(color) | |
.addAction(action); | |
notificationManager.notify(1000, notification.build()); | |
} | |
private PendingIntent getPendingIntent(Context context, String message) { | |
Intent intent = new Intent(context, Main2Activity.class); | |
intent.putExtra("MESSAGE", message); | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); | |
stackBuilder.addParentStack(Main2Activity.class); | |
stackBuilder.addNextIntent(intent); | |
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); | |
} | |
private PendingIntent getPendingIntentURL() { | |
String url = "http://benzneststudios.com"; | |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); | |
return PendingIntent.getActivity(MainActivity.this, 0, intent, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment